美文网首页
静态方法

静态方法

作者: 平凡的鱼仔 | 来源:发表于2018-11-29 22:14 被阅读4次

c#静态方法特点;

  • 1 静态方法属于某个类,不属于某一个对象,所有对象公用一个副本。
  • 2 静态方法只能访问静态成员。
  • 3 静态方法不需实例化便可以使用,即创建对象前便可以使用。
  • 4 静态方法中不能使用this关键字。

'''

class Test
{
    private static int num = 2;
    private int part = 10;

    public static int getDoubleNum()
    {
       // part += 5; 报错,不能访问非静态成员
       //this.num=5;报错,不能使用this关键字
        return 2 * num;
    }
    static void Main(string[] args)
    {
        Console.ReadKey();
    }
}

'''

相关文章

网友评论

      本文标题:静态方法

      本文链接:https://www.haomeiwen.com/subject/ewqqcqtx.html