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