using System;
Class Test
{
int x;
static int y;
void f(){
x = 1;//等价this.x=1
y = 1; //等价Test.y=1
}
static void g(){
x=1;//错误,不能访问this.x
y=1;//正确,等价Test.y=1
}
static void Main(){
Test t=new Test();
t.x=1;
t.y=1;//错误,不能在类的实例中访问静态成员
Test.x=1;//错误,不能按类访问非静态成员
Test.y=1;//正确
}
}
参见:asp.net中Static的用法
本文通过具体的C#代码示例介绍了Static关键字的使用方法,包括静态成员变量、静态方法和实例方法的区别及如何正确访问。
1232

被折叠的 条评论
为什么被折叠?



