码迷,mamicode.com
首页 > 其他好文 > 详细

Static

时间:2015-04-14 00:25:11      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

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!

技术分享

 

Static

标签:

原文地址:http://www.cnblogs.com/gaoxianzhi/p/4423499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!