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
广告
数据结构
网络技术
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP