标签:
1. C++
static has three methods to use.
a. external no need to add static. but it is stored statically.
b. internal need to add static. it si stored statically. but we can set a variable with the same name in other files.
c. non linkage. used in a function. it can‘t be accessed when the function is not called. but when the function is called again, the value will keep the previous one before it is changed in this calling.it is stored statically.
voidStringChapter::staticTest()
{staticint id2 = 0;id2++;
}
myapp->staticTest();
myapp->staticTest();
myapp->staticTest();
myapp->staticTest();
in the forth calling of statictest, the value of id2=4;
it means in c++, the static variable will be only initialize once. But can be assigned for several times!
it means, the static variable will keep to change its value since staticint id2 = 0; is called for several times.
2. C#
a. static field.
if is not assigned be a initial value. it woud be set to 0 null false. it belong to the class instead of instance.
they would be initialized once any static fields of this class is calling.
1.1static readonly int a[];
1.2a=new a[10];
1.3a =new a [11]; //exception. static field can not be new twice.
I made a mistake for so long a time. Only readonly can be assign once. not static.....................................................!
b. static method.
if you want to use instance method. please get a instance at first. (from arguments)
if you want to use instance field, please get a instance at first as well.
c. static constructor.
it is use to initialize the class, not instance. after self initialize, the static field will be initialize in the static class.
It would be called at the first calling of this class.
d. static attribute.
e. static class
abstact sealed.
f. none linkage. Error!
标签:
原文地址:http://www.cnblogs.com/gaoxianzhi/p/4423499.html