码迷,mamicode.com
首页 > Windows程序 > 详细

static(C# 参考)

时间:2018-01-20 22:50:57      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:范围   void   ber   stat   访问   静态   .com   类实例化   ase   

 

使用 static 修饰符声明属于类型本身而不是属于特定对象的静态成员。 static 修饰符可用于类、字段、方法、属性、运算符、事件和构造函数,但不能用于索引器、析构函数或类以外的类型。 有关更多信息,请参见 静态类和静态类成员

示例

 

下面的类声明为 static,并且只包含 static 方法:

C#

1     static class CompanyEmployee
2     {
3         public static void DoSomething() { /*...*/ }
4         public static void DoSomethingElse() { /*...*/  }
5     }

 

常数或者类型声明隐式地是静态成员。

不能通过实例引用静态成员。 然而,可以通过类型名称引用它。 例如,请考虑以下类:

C#

 

1    public class MyBaseC
2     {
3         public struct MyStruct
4         {
5             public static int x = 100;
6         }
7     }

 

若要引用静态成员 x,请使用完全限定名 MyBaseC.MyStruct.x,除非可从相同范围访问成员:

C#

Console.WriteLine(MyBaseC.MyStruct.x); 

尽管类的实例包含该类所有实例字段的单独副本,但每个静态字段只有一个副本。

不可以使用 this 来引用静态方法或属性访问器。

如果对类应用 static 关键字,则该类的所有成员都必须是静态的。

类和静态类可以有静态构造函数。 静态构造函数在程序开始和类实例化之间的某个时刻调用。

 说明

static 关键字在使用上比在 C++ 中有更多限制。 若要与 C++ 关键字比较,请参见 Static

为了说明静态成员,请看一个表示公司雇员的类。 假设该类包含一种对雇员计数的方法和一个存储雇员数的字段。 该方法和字段都不属于任何实例雇员, 而是属于公司类。 因此,应该将它们声明为此类的静态成员。

示例

 

此示例读取新雇员的姓名和 ID,将雇员计数器加一,并显示新雇员的信息和新的雇员数。 为简单起见,该程序从键盘读取当前的雇员数。 在实际的应用中,应从文件读取此信息。

C#

    

 1 public class Employee4
 2 {
 3     public string id;
 4     public string name;
 5 public Employee4()
 6     {
 7     }
 8 public Employee4(string name, string id)
 9     {
10         this.name = name;
11         this.id = id;
12     }
13 public static int employeeCounter;
14 public static int AddEmployee()
15     {
16         return ++employeeCounter;
17     }
18 }
19 class MainClass : Employee4
20 {
21     static void Main()
22     {
23         Console.Write("Enter the employee‘s name: ");
24         string name = Console.ReadLine();
25         Console.Write("Enter the employee‘s ID: ");
26         string id = Console.ReadLine();
27 // Create and configure the employee object:
28         Employee4 e = new Employee4(name, id);
29         Console.Write("Enter the current number of employees: ");
30         string n = Console.ReadLine();
31         Employee4.employeeCounter = Int32.Parse(n);
32         Employee4.AddEmployee();
33 // Display the new information:
34         Console.WriteLine("Name: {0}", e.name);
35         Console.WriteLine("ID:   {0}", e.id);
36         Console.WriteLine("New Number of Employees: {0}",
37                       Employee4.employeeCounter);
38     }
39 }
40     /*
41     Input:
42     Matthias Berndt
43     AF643G
44     15
45      * 
46     Sample Output:
47     Enter the employee‘s name: Matthias Berndt
48     Enter the employee‘s ID: AF643G
49     Enter the current number of employees: 15
50     Name: Matthias Berndt
51     ID:   AF643G
52     New Number of Employees: 16
53     */

 

示例

 

此示例说明:虽然可以用另一个尚未声明的静态字段实例化一个静态字段,但直到向后者显式赋值后,才能确定结果。

C#

 1     class Test
 2     {
 3        static int x = y;
 4        static int y = 5;
 5 static void Main()
 6        {
 7           Console.WriteLine(Test.x);
 8           Console.WriteLine(Test.y);
 9 Test.x = 99;
10           Console.WriteLine(Test.x);
11        }
12     }
13     /*
14     Output:
15         0
16         5
17         99
18     */

 

 

来自 <https://msdn.microsoft.com/zh-cn/library/98f28cdx.aspx>

 

static(C# 参考)

标签:范围   void   ber   stat   访问   静态   .com   类实例化   ase   

原文地址:https://www.cnblogs.com/pugongying123/p/8322073.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!