什么是C#中的静态成员函数?
静态函数只能访问静态变量。静态函数甚至在创建对象之前就已存在。
将静态函数设置为-
public static int getNum() {}
以下是演示静态函数用法的示例-
示例
using System;namespace Demo {
class StaticVar {
public static int num;
public void count() {
num++;
}
public static int getNum() {
return num;
}
}
class StaticTester {
static void Main(string[] args) {
StaticVar s = new StaticVar();
s.count();
s.count();
s.count();
Console.WriteLine("Variable num: {0}", StaticVar.getNum());
Console.ReadKey();
}
}
}
输出结果
Variable num: 3
以上是 什么是C#中的静态成员函数? 的全部内容, 来源链接: utcz.com/z/327159.html