C # Keywords: static

A, static keyword

  Now I designed a class of mortgage interest rates go up (used to calculate how much mortgage interest rates go up):

. 1  public  class InterestRateGoUp
 2  {
 . 3      public InterestRateGoUp ()
 . 4      {
 . 5          Rate = 4.9m ;
 . 6      }
 . 7  
. 8      ///  <Summary> 
. 9      /// benchmark rate
 10      ///  </ Summary> 
. 11      public  decimal Rate { GET ; SET ;}
 12 is  
13 is      ///  <Summary> 
14      /// calculated float
 15      ///  </ Summary> 
16      /// <param name = "rate"> current mortgage rates </ param> 
. 17      ///  <Returns> return floating interest rates Results </ Returns> 
18 is      public  decimal CalculatorGoUp ( decimal Rate)
 . 19      {
 20 is          decimal Result = (Rate - Rate) / rate Penalty;
 21 is          return Result;
 22 is      }
 23 is  }
 24  public  class RealizeObject
 25  {
 26 is      public the Realize ()
 27      {
 28          // small black buy rates 6.37 
29          InterestRateGoUp rate1 =new new InterestRateGoUp ();
 30          a float RESULT1 rate1.CalculatorGoUp = ( 6.37m );
 31 is          // adzuki buy rates 6.125 
32          InterestRateGoUp rate2 = new new InterestRateGoUp ();
 33 is          a float result2 = rate2.CalculatorGoUp ( 6.125m );
 34 is          // Mortgage reference rate becomes 5.88, the benchmark for all the objects need to change the rate of 
35          rate1.Rate = 5.88m ;
 36          rate2.Rate = 5.88m ;
 37 [          // if there are hundreds or thousands of such objects have to be modified to the one hundred thousand 
38      }
 39 }

  The problem lies in the example above, this property is the benchmark interest rate mortgage interest rates go up all objects share attributes, rather than mortgage interest rates go up every object has a benchmark interest rate. So put the benchmark interest rate this property to a shared need to use the static keyword, Second Edition mortgage interest rates go up categories:

 1 public class InterestRateGoUp
 2 {
 3     public InterestRateGoUp()
 4     {
 5         Rate = 4.9m;
 6     }
 7 
 8     /// <summary>
 9     /// 基准利率
10     /// </summary>
11     public static decimal Rate { get; set; }
12 }
13 public class RealizeObject
14 {
15     public Realize()
16      {
 17          // mortgage reference rate becomes 5.88, only need to modify the static properties (all objects are using it), using the following static property 
18 is          InterestRateGoUp.Rate = 5.88m ;
 . 19          // use constructor initializes a static property exists the problem is that each time you create an object will initialize static properties, as will modify its benchmark interest rate to 4.9 
20          InterestRateGoUp rate3 = new new InterestRateGoUp ();
 21      }
 22 }

   Static automatic initialization attribute two, the first automatic static properties to static properties (a static properties valued package private static fields), the second is the use of static constructors, class Third Edition mortgage rates go up :

. 1  public  class InterestRateGoUp
 2  {
 . 3      ///  <Summary> 
. 4      /// method: static properties, rather than static automatic properties
 . 5      ///  </ Summary> 
. 6      Private  static  decimal Rate = 4.9m ;
 . 7      public  static  decimal Rate
 . 8      {
 . 9          GET { return Rate;}
 10          SET {Rate = value;}
 . 11      }
 12 is  
13 is      ///  <Summary> 
14      /// method two: static constructors
15     /// </summary>
16     static InterestRateGoUp()
17     {
18         Rate = 4.9m;
19     }
20 }

   If the calculated floating CalculatorGoUp method is often used, can CalculatorGoUp set static; mortgage interest rates go up if the whole class is a special class tools for users, you can put all the classes and class members set to static. The final version of the mortgage interest rates go up categories:

. 1  public  static  class InterestRateGoUp
 2  {
 . 3      static InterestRateGoUp ()
 . 4      {
 . 5          Rate = 4.9m ;
 . 6      }
 . 7  
. 8      ///  <Summary> 
. 9      /// benchmark rate
 10      ///  </ Summary> 
. 11      public  static  decimal Rate { GET ; SET ;}
 12 is  
13 is      ///  <Summary> 
14      /// calculated float
 15      ///  </ Summary> 
16     ///  <param name = "Rate"> current mortgage rates </ param> 
. 17      ///  <Returns> return floating interest rates Results </ Returns> 
18 is      public  static  decimal CalculatorGoUp ( decimal Rate)
 . 19      {
 20 is          decimal Result = (Rate - rate) / rate;
 21 is          return Result;
 22 is      }
 23 is  }
 24  public  class RealizeObject
 25  {
 26 is      public the Realize ()
 27      {
 28          // small black buy rates 6.37 
29         a float RESULT1 InterestRateGoUp.CalculatorGoUp = ( 6.37m );
 30          // adzuki buy rates 6.125 
31 is          a float result2 = InterestRateGoUp.CalculatorGoUp ( 6.125m );
 32          // mortgage reference rate becomes 5.88 
33 is          InterestRateGoUp.Rate = 5.88m ;
 34 is          // Buy rates Jill 5.88 
35          a float result3 InterestRateGoUp.CalculatorGoUp = ( 5.88m );
 36      }
 37 [ }

 to sum up:

1, static keyword may be modified on the class and type members (fields, properties, methods, constructors, etc.).

2, static keyword modified content will be created once and only once allocated memory is recovered (do not take performance GC) until the end of the program. static keyword modified type members is on (it is the only) target level rather than on a class level, it is only through the use of static members (class name. static members) and not through (object name. static members) static class can not create their objects using the new.

3, examples of field / properties are exclusive of each object, each object will be to their own memory space allocated for each instance of data, and the static field / property is shared by all objects, no matter how many objects are created, assigned static data only once . Static method is suitable for use under circumstances often can reduce the number of memory allocation and cleanup. Instance fields class / instance attributes and methods can not be used directly in a static method, by using only (object name .XXX) manner. Exclusively for static constructor initializes the static data, where it is more suitable for unknown at compile-time initialization value of the static data (such as a database read). Also known as a static class tools for the entire class often use, you can not use it to create new, and can only contain modified with static type members.

Remarks:

1, a static constructor can be defined without reference static constructor and does not allow access modifiers;

2, no matter how many types of objects to create a static constructor is executed only once;

3, create a runtime instance of the class or the caller for the first time before accessing static members, the runtime calls the static constructor;

4, performing a static constructor prior to the non-static constructor.

 

Part of references in the sixth edition proficient in C #

Guess you like

Origin www.cnblogs.com/yaojieyuan/p/11502412.html