Use a summary of the attributes in the C class (details the attributes of the class)

  • 2020-06-15 10:08:29
  • OfStack


private int dd;  
public int dd  
{  
    get{ return xx*3;}  
    set{ xx = value/3;}  
}  

An attribute without set is a read-only attribute, and an accessor without get is a write-only attribute.
(1) The get accessor is used to return a field or to calculate and return a field and must terminate with return or throw.

private string name;  
public string Name  
{  
    get  
    {  
        return name != null ? name : "NA";  
    }  
}  

(2) The set accessor is similar to a function of return type void, using the implicit argument of value

private string name;  // the name field  
    public string Name    // the Name property  
    {  
        get  
        {  
            return name;  
        }  
        set  
        {  
            name = value;  
        }  
    } 

(3) Access restrictions
Property access tags for public private, protected, internal, protected internal, because the visitor access restrictions must be more strict than attribute access restrictions, so

private int xx;  
      public int sxx  
      {  
          public get { return xx; }//error  
          set { xx = value; }  
      }  

You cannot use access modifiers on interfaces or explicit interfaces because all the defaults in the interface are public;
Access modifiers are allowed only when get and set accessors are available, and only one of them can be used.
If the attribute has an override modifier, the accessor modifier must match the overridden one.
The accessibility level of an accessor must be more stringent than the accessibility level of an attribute

To understand:
First of all, article 4 is the easiest to think about, and it makes sense, because it is, after all, an external decision and an internal one.
Second, since article 4 is understandable, if there is only one accessor and the accessor accesses the same level as the attribute, then why not specify it on the attribute?
This makes sense, so it is more clear why you must have get and set to add access modifiers.

Reasoning:
The attribute in the interface is public, so if there are only 1 get or set in the interface, we can specify the ownership of another accessor in inheritance. However, if the interface has both get and set, then by derivation and inheritance matching, it is no longer possible to specify accessor access restrictions in this way.


public interface ISomeInterface  
{  
    int TestProperty  
    {  
        // No access modifier allowed here  
        // because this is an interface.  
        get;  
    }  
}  

  
public class TestClass : ISomeInterface  
{  
    public int TestProperty  
    {  
        // Cannot use access modifier here because  
        // this is an interface implementation.  
        get { return 10; }  

  
        // Interface property does not have set accessor,  
        // so access modifier is allowed.  
        protected set { }  
    }  
}  

(4) static can be used to modify the attribute so that it can be accessed at any time


private static int counter;  
public static int Counter  
    {  
        get { return counter; }  
    }  

(5) Attribute hiding

public class Employee  
{  
    private string name;  
    public string Name  
    {  
        get { return name; }  
        set { name = value; }  
    }  
}  

  
public class Manager : Employee  
{  
    private string name;  

  
    // Notice the use of the new modifier:  
    public new string Name // use new to hide property in base class  
    {  
        get { return name; }  
        set { name = value + ", Manager"; }  
    }  
}  

(6) virtual to modify the attribute, derived classes use override to override the attribute


public class Parent  
{  
    public virtual int TestProperty  
    {  

        protected set { }  
        get { return 0; }  
    }  
}  
public class Kid : Parent  
{  
    public override int TestProperty  
    {  
        protected set { }  
        get { return 0; }  
    }  
}  

(7) abstract modifies attributes and derives classes to implement attributes


abstract class Shape  
{  
    public abstract double Area  
    {  
        get;  
        set;  
    }  
}  

  
class Square : Shape  
{  
    public double side;  
    public override double Area  
    {  
        get  
        {  
            return side * side;  
        }  
        set  
        {  
            side = System.Math.Sqrt(value);  
        }  
    }  
}  

(8) sealed modifies the attribute. Derived classes cannot modify the attribute

(9) Interface properties
The interface property does not have a function body


public interface Inters  
{  
    string Name  
    {     
        get;  
        set;  
    }  
}  

(10) Automatic properties
When no additional access logic is needed in the property accessor, automatic properties can be used to make the code cleaner


private string name;  
public string Name  
{  
    get  
    {  
        return name != null ? name : "NA";  
    }  
}  
0


Related articles: