In-depth analysis of C # inheritance mechanism 6

xiaoxiao2021-03-06  52

V. Inheritance and Access Demo Access Molding is some keywords for specifying members or types of access to the declaration. There are four access modifiers in the inheritance of the class: public protected interface privics. Use these access modifiers to specify the following five accessibility levels: Public Protected Internal protected private.

Declare accessibility PUBLIC access is unrestricted. Protected Access is limited to the type containing classes or derived from the included class. INTERNAL access is limited to the current project. Protected Internal Access is limited to the current project or type of derived generation. The Private Access is limited to the type of included.

1. Inherited all members of the problem of visible domain (except for instance constructors, destructors, and static constructors) inherited by derived types. This even includes a private member of the base class. However, the accessible domain of private members only includes program text that declares the type of the member. In the example below

Class a {int x; static void f (b) {b.x = 1; //}} Class B: a {static void f (b) {b.x = 1; // error}}

The class B inherits the private member X of class A. Because the member is private, it can only be accessed in A "Cyber". Therefore, the access to B.X has achieved success in the A.f method, but it failed in the B.f method. 2. Inherited some of the issues of attributes and members of the class, we can also define the overload, virtual property, abstract properties, and seal properties. Like a class and method, the modification of the attribute should also meet the following rules: the overload of the property 1. Use the properties of the modifier in the derived class, indicating that the same name properties in the base class are overloaded. 2. In the overloaded statement, the name, type, access modifier of the attribute should be consistent with the attributes inherited in the base class. 3. If the property of the base class has only one attribute accessor, the attribute after the heavy-duty should also have one. However, if the attributes of the base class include the GET and SET attribute accessor, the overloaded properties can only be one or two attribute accessors. Note: Different from method overload is that the overload declaration of the attribute does not actually declare the new attribute, but only provides the specific implementation of the accessor for existing virtual properties. During virtual properties 1. Use the Virtual modifier declaration to the virtual property. 2. Virtual accessor includes GET Accessor and SET Accessor, which is also virtual. Abstract properties 1. Use the Abstract modifier declaration with the properties of the abstract properties. 2. The accessor of abstract attributes is also virtual, and there is no specific implementation of the accessor. This requires the specific implementation of the accessor by the derived class by the derived class by the derived class. 3. Use the Abstract and Override modifiers, not only indicate that the attribute is abstract, but also. Moreover, it is overloaded the virtual property in the base class This time the accessor of the attribute is also abstract. 4. Abstract properties are only allowed to be declared in an abstract class. 5. In addition to using Abstract and Override modifiers, Static, Virtual, Override and Abstract modifiers can no longer appear again. Sealed Attributes 1. The properties that use the SeaD modifier are sealing properties. The sealing properties of the class are not allowed in the derived class. The accessor of the seal attribute is also sealed. 2. If there is a Sealed modifier when declared, there must be an Override modifier. As can be seen from the above, these rules of the attribute are very similar to the method. For the attribute accessor, we can view the GET accessor as a method of type with the property modifier, no parameters, return value attribute, and see the SET Accessor as an attribute modifier, only Contains a value parameter, return the type of VOID. Look at the procedure below:

using System; public enum sex {woman, man,}; abstract public class People {private string s_name; public virtual string Name {get {return s_name;}} private sex m_sex; public virtual sex Sex {get {return m_sex;} protected String s_card; public abstract string card {get; set;}} Declare "People" class, people's name Name and gender SEX are two read-only virtual properties: ID number card is an abstract property Allow read and write because the class people contains an abstract attribute Card, so people must declare to abstraction. Below we write a class class inheritance from people. Look at the following procedure:

Class Customer: People {string S_NO; INT i_DAY; Public String No {Get {Return S_NO;} S_NO = Value;}} public int day {rary}}} set {i (} set {= ! i_day = value) i_day = value;}} public override string Name {get {return base.Name;}} public override sex Sex {get {return base.Sex}} public override string Card {get {return s_ card;} Set {s_ card = value;}}}

In Class Customer, the statement of the attribute Name, SEX, and CARD adds an Override modifier, and the attribute declaration is consistent with the base class PEOPLE. Name and SEX GET Accessor, Card's GET and SET Accessors use the base keyword to access the accesser properties in the base class PEOPLE. Card's statement overloads an abstract accessor in the base class. In this way, there is no abstract member in the Customer class, and the Customer can be non-virtual.

转载请注明原文地址:https://www.9cbs.com/read-83903.html

New Post(0)