Static

xiaoxiao2021-03-06  128

Use the Static modifier to declare a static member that belongs to the type itself instead of a specific object. Static modifiers can be used for fields, methods, attributes, operators, events, and constructors, but cannot be used for indexers, destructuring functions, or types.

Note

Constant or type declaration is implicitly a static member. Static members cannot be referenced by examples. However, it can be referenced by the type name. For example, consider the following: public class mybasec

{

Public struct mystruct {

Public static int x = 100;

}

} To reference static members x, use full qualitative names (unless you can access from the same range): MYBASEC.MYSTRUCT.X Although the class's instance contains separate copies of the class all instance fields, each static field has only one copy. You cannot use this to reference a static method or attribute accessor.

Note that Static keywords are more restrictions in use than in C . To compare with C keywords, see C Language Reference

Static

.

To illustrate the instance member, please see a class that represents the company's employee. Assume that the class contains a method for the employee count and a field that stores the number of employees. This method and field are not part of any instance employee, but is a company class. Therefore, they should be declared as static members of this class.

For more information on constructor, see

10.10 instance constructor

.

Example

This example reads the name and ID of the new employee, adds the employee counter and displays the information about the new employee and the number of new employees. For the sake of simplicity, the program reads the current employee from the keyboard. In actual applications, this information should be read from the file.

// CS_STATIC_KEYWORD.CS

// static members

Using system;

Public Class Employee

{

Public String ID;

Public String Name;

Public Employee ()

{

}

Public Employee (String Name, String ID)

{

THIS.NAME = Name;

THIS.ID = ID;

}

Public Static int Employeecounter;

Public static int addEMPLOYEEEEE ()

{

Return Employeecounter;

}

}

Class Mainclass: Employee

{

Public static void main ()

{

Console.write ("Enter the Employee's Name:");

String name = console.readline ();

Console.write ("Enter the Employee's ID:");

String id = console.readline ();

// Create The Employee Object:

Employee E = New Employee (Name, ID);

Console.Write ("Enter the current number of employees:");

String n = console.readline ();

Employee.employeecounter = int32.parse (n);

Employee.adDemployee ();

// Display the new information:

Console.writeline ("Name: {0}", E.NAME);

Console.Writeline ("ID: {0}", E.ID); console.writeline ("New Number of Employees: {0}",

Employee.employeecounter;

}

}

enter

Tara Strahan

AF643G

15

Example output

Enter the Employee's Name: Tara Strahan

ENTER THE EMPLOYEE'S ID: AF643G

ENTER THE CURRENT NUMBER OF EMPLOYEES: 15

Name: Tara Strahan

ID: AF643G

New Number of Employees: 16

See

C # Keyword | modifier

转载请注明原文地址:https://www.9cbs.com/read-99868.html

New Post(0)