【问题标题】:Declaration of class as static and non static [duplicate]将类声明为静态和非静态[重复]
【发布时间】:2017-04-12 22:09:17
【问题描述】:

这是我的课:

 public  class TestClass
 {
    public static int one;
 }

这样声明有什么区别:

public static TestClass test;

还有这个:

public TestClass test;

【问题讨论】:

    标签: c# static non-static


    【解决方案1】:

    你可以找到答案here

    静态方法、字段、属性或事件甚至可以在类上调用 当没有创建类的实例时。如果有任何实例 类已创建,它们不能用于访问静态 成员。仅存在一份静态字段和事件的副本,并且是静态的 方法和属性只能访问静态字段和静态 事件。静态成员通常用于表示数据或 不响应对象状态而改变的计算;为了 例如,数学库可能包含用于计算的静态方法 正弦和余弦。静态类成员使用静态声明 成员返回类型前的关键字,例如:

    public class Automobile {
        public static int NumberOfWheels = 4;
        public static int SizeOfGasTank
        {
            get
            {
                return 15;
            }
        }
        public static void Drive() { }
        public static event EventType RunOutOfGas;
    
        //other non-static fields and properties... }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      相关资源
      最近更新 更多