Two use C # static field

Static fields 2 commonly used method

(1) the number of records of the object has been instantiated

(2) memory must be shared among all instances of values

  

  (1) the number of records of the object has been instantiated

Now some training institutions ah, to open a school class barber, plans to recruit 5 people, as long as enough number 5 person classes.

But the students one by one, can not suddenly put 5 individuals attracted over.

Then move the first person, the total number of students is 1 , then recruit two people, and that the total number of students is 3 , and that is the total number of students vary, the total number of students will need to be declared as static fields, because the static field is instantiated only It will use the same area (just change the value stored Bale) in memory. If you declare an instance of a member, it is in trouble, we need to open up 5 two memory space, Nima really rich ah!

 

 

 

 

 

( 2 ) memory must be shared among all instances of values

Now the classes, the class teacher is only one, called monk. Five teacher told the class knows monk ( that is to say the teacher in this field is shared oh ) . But one day the daughter of the monk to go to the wife's mother to go home, classes can not stop it. So please Goddess of Mercy sister came and took some lessons.

Now 5 students know that the teacher replaced Goddess of Mercy sister, very excited and Kazakhstan. But they know is how to change the teacher do? This credit or static field, the teacher's name declared as a static field, which is shared. So this kind of property can know.

Ado, directly on the code ;

 

 

Guess you like

Origin www.cnblogs.com/schangxiang/p/11183795.html