[.net Basics of Object-Oriented Programming] (9) Class members (fields, properties, methods)

This article is transferred from: https://www.cnblogs.com/yubinfeng/p/4552717.html

The class of Person defined above includes members: fields, attributes, methods, events, etc. In addition, the nested classes mentioned above are also members of the class. 

  a. The members of the class are divided into: static members (static) and non-static members 

  b. Static members are marked with static, if not marked, the default is non-static members 

  c. Static members belong to the class, dynamic members belong to the instance, that is, the object 

  d. The static member is shared by all instances of the class. No matter how many instances or copies of the class, the static member only occupies an area in the storage. Non-static members create a memory domain in each instance of the class.    

 

The following mainly explains the main members of the class: fields, attributes, methods 

 

1. Members of the class-field 

   Field declaration: (static / readonly) <Type> <variable name>    

   a. It can be understood as a private variable of the class, which is usually private. 

   b. The definition of the field usually starts with a lowercase letter or "_". 

   c. There are two types of field declaration modifiers: static (static) and readonly (read-only). 

   The d field is usually private, so access modifiers are generally not needed. 

   Examples:     

1    static int eyesCount=2;
2    readonly int earsCount=2;  

  

2. Members of the class-properties 

   a. Can be understood as a public variable of the class, usually all public (public) 

   b. The attribute has two methods, get and set. 

   The c.get accessor returns the same data as the attribute declaration type, meaning that it can get the value or reference of the internal field when called. 

   The d.set accessor does not display the set parameters. It has an implicit parameter value. Its function is to assign values ​​to internal fields or references of the property when it is called. 

   e. Because the members of the class are private by default, because the attribute is public, in the object-oriented development process, you must use the modifier public to declare an attribute as public. 

   f. Attributes can ignore get or set accessors, but not both. 

   Examples:     

Copy code
1 string _country; 
 2 // read-write attribute 
 3 public string Country 
 4 { 
 5 set {_country = value;} 
 6 get {return _country;} 
 7} 
 8 // read-only attribute 
 9 public string CountryOnleread 
10 { 
11 get {return _country ;} 
12}         
13 // write only attribute 
14 public string Countryonlywrite 
15 { 
16 set {_country = value;} 
17}
Copy code

 

6. Members of the class-methods (Method) 

      Declaration: (access modifier) ​​<type> <method name> {method body} 

     Call: [<class name.>] | [<Instance object name.>] <Method name> ([<real parameter list>]) 

     Definition: is a member of the class used to perform calculations or other behaviors

 

Static methods:
     methods are divided into instance methods and static methods (same as the members of the class mentioned earlier) 

     Only static fields can be called in static methods, non-static fields are not allowed 

Method parameters:

      Value parameter : without any modifiers. The formal parameter in the method is a copy of the actual parameter. The change of the formal parameter will not affect the value of the actual parameter in the memory. The actual parameter is safe. 

     Reference parameters : declared with the ref modifier. 

    The ref keyword causes parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes made to the parameters in the method will be reflected in the variable.
    To use the ref parameter, both the method definition and the calling method must explicitly use the ref keyword.
    The parameters passed to the ref parameter must be initialized first. This is different from out, the parameters of out do not need to be initialized explicitly before being passed.
    Attributes are not variables, so they cannot be passed as ref parameters.
    Although ref and out are handled differently at runtime, they are treated the same at compile time. Therefore, if one method takes the ref parameter and the other method takes the out parameter, you cannot overload these two methods. For example, from a compilation perspective, the two methods in the following code are identical. If you try to do this, the code will not compile.
    If one method takes ref or out parameters, and the other method does not take these two types of parameters, it can be overloaded.

   Code example: 

Copy code
// Call 
double [] numbers = new double [] {1, 2, 3, 5.5}; 
double i = 0; 
MyAddOperation (numbers, ref i);             
Console.WriteLine ("2 times the calculation result is: {0} ", i * 2); 
Console.ReadLine (); 

// Reference parameter method declaration 
public static void MyAddOperation (double [] numbers, ref double result) 
{ 
    result = 0; 
    foreach (double num in numbers) 
        result + = num; 
    Console.WriteLine ("The calculation result is: {0}", result); 
}
Copy code

  

 Output parameters : declared with the out modifier. Similar to ref, it also directly operates on actual parameters. The out keyword must be specified explicitly in both method declaration and method invocation. The out parameter declaration method does not require the variable to be initialized before being passed to the method, because its meaning is only used for output purposes. However, before the method returns, the out parameter must be assigned. 
The out keyword causes parameters to be passed by reference. This is similar to the ref keyword. 


Differences from ref: 
ref requires that variables must be initialized before being passed. 
Although variables passed as out parameters do not need to be initialized before being passed, methods need to be called to assign values ​​before the method returns. 

Examples: 

Copy code
1 // Call 
 2 double [] numbers = new double [] {1, 2, 3, 5.5}; 
 3 double i = 0; 
 4 MyAddOperation (numbers, out i);             
 5 Console.WriteLine ("2 times the calculation result Yes: {0} ", i * 2); 
 6 Console.ReadLine (); 
 7 
 8 // Output parameter method declaration 
 9 public static void MyAddOperation (double [] numbers, out double result) 
10 { 
11 result = 0; 
12 foreach (double num in numbers) 
13 result + = num; 
14 Console.WriteLine ("The calculation result is: {0}", result); 
15}
Copy code

 

 Array parameters : declared with the params modifier. The params keyword is used to declare a variable length parameter list. Only one params parameter can be included in the method declaration. The params parameter is very useful when the number of parameters is variable, see the following example:

 

Copy code
1 // Call method 
 2 double [] numbers = new double [] {1, 2, 3, 5.5}; 
 3 Console.WriteLine ("The calculation result is: {0}", MyAddOperation (numbers)); 
 4 
 5 // Array type parameter declaration 
 6 public static double MyAddOperation (params double [] numbers) 
 7 { 
 8 double result = 0; 
 9 foreach (double num in numbers) 
10 result + = num; 
11 return result; 
12}
Copy code

 

Virtual method (virtual method) 

     The virtual keyword is used to modify methods in the base class. There are two situations for the use of virtual:  

    Case 1: The virtual method is defined in the base class, but the virtual method is not overridden in the derived class. Then in the call to the derived class instance, the virtual method uses the method defined by the base class. 

    Case 2: A virtual method is defined in the base class, and then override it in the derived class using override. Then in the call to an instance of a derived class, the virtual method uses a derived overriding method. 

Abstract method (abstract method) 

     The abstract keyword can only be used to modify methods in abstract classes, and there is no specific implementation. The implementation of abstract methods must be implemented using the override keyword in the derived class. 

(About the abstract class will be explained in detail later)

 

Key points: 

1. Only static fields can be called in static methods, non-static fields are not allowed 

2. Method with no return value, type void  

Guess you like

Origin www.cnblogs.com/hanguoshun/p/12728358.html