Object-oriented-inheritance (you must know.) NET reading notes)

Source: Internet
Author: User

1. What is inheritance
Inheritance is a kind of relationship between class and class in object-oriented
Implementation of inheritance: Implementing Inheritance Interface Inheritance

2. The nature of inheritance
(1) Inheritance is transitive, subclasses are extensions to the parent class, methods of the parent class must be inherited, and new methods can be added
(2) Subclasses can invoke parent class methods and fields, and parent classes cannot call child class methods and fields
(3) How the virtual method implements the overwrite operation so that the parent pointer can point to the child class object member
(4) Like not only the public members of the parent class are inherited, but also the private members of the parent class are inherited, but not accessed in the same
(5) The blocking effect of new keyword in virtual method inheritance

Example Description:

1 Internal class Program2     {3         Private Static voidMain (string[] args)4         {5Bird bird=NewBird ();6             //briefly describe the creation process:7             /*8 (1) Bird Bird creates a reference to a Bird type, and new Bird () completes the creation of the Bird object,9 * Allocate memory space and initialization operations, then assign this object reference to the bird variable, which is to establish the association of the bird variable with the bird objectTen * The essence of inheritance is embodied in the process of creating objects: we use the creation of chicken objects as an example One * 1. Field, once created, will first find its parent class bird and allocate storage space for its fields, while Bird continues to find the parent class animal, for which A * Allocate space, and so on recursively end, that is, complete System.Object memory allocation. - * 2. Method table creation, method table creation is the first time the class loads the large AppDomain, when the object is created just attach its attached member Typehandle to the address of the method list on the loader heap, associate the object with the dynamic method list, so the method A table exists before an object.  - * Method table creation similar to the creation of a field, class chicken generate a method list, first copy all the virtual methods of bird, and then compare with Chicken's own method list, if the overridden virtual method overrides the parent class method of the same name with the subclass method, and adds the subclass new party method to complete the list of chicken methods.  the              */ -Chicken chicken=NewChicken (); -Bird Bird2 =NewChicken (); -             varType=Bird2.type; + Console.read (); -             /* + Questions: A * Bird bird2=new Chicken (); at * In this case, Bird2. What does ShowType return? What is Bird2.type?  - * Solve this problem with 2 principles: - * Focus on the object principle: A method called or a parent class, depending on whether the object being created is a subclass or a parent class object, rather than its reference type.  - * For example: Bird bird2=new Chicken (); We are concerned with creating objects as Chicken types, so subclasses inherit the fields and methods of the parent class, or override the virtual methods of the parent class, regardless of whether the Bird2 reference type is Bird. The difference between reference types determines how different objects have different access rights in the method table - * Implementation of the nearest principle: for the same name field or method, the compiler is in order to find the reference, that is, the first access to the field or method created near it, such as the above Bird2, is the bird type, so the first access to Bird_type, if the type is publ IC, return "Bird" value -              */ in         } -     } to  +      Public Abstract classAnimal -     { the          Public Abstract voidShowType (); *  $          Public voidEat ()Panax Notoginseng         { -Console.WriteLine ("animals have the act of eating."); the         } +     } A  the      Public classBird:animal +     { -          Public stringType ="Bird"; $          Public Override voidShowType () $         { -Console.WriteLine ("Type is {0}", type); -         } the  -         Private stringcolor;Wuyi  the          Public stringColor -         { Wu             Get -             { About                 returncolor; $             } -             Set -             { -color =value; A             } +         } the     } -  $      Public classChicken:bird the     { the          Public stringType ="Chicken"; the          Public Override voidShowType () the         { -Console.WriteLine ("Type is {0}", type); in         } the  the          Public voidShowcolor () About         { theConsole.WriteLine ("Color is {0}", Color); the         } the}
View Code


3. Issues of succession
(1) Inheritance may cause infinite expansion of subclasses, which is detrimental to the maintenance and security of the class system.
(2) The inherited subclass object is determined at compile time, cannot satisfy the situation that needs to run the period to determine, and the class aggregation solves this problem well
(3) With the complexity of the inheritance hierarchy and the diversification of subclasses, it is inevitable that invalid inheritance or harmful inheritance will occur to the parent class.
4. Summary
(1) Sealed class can not be inherited
(2) In the inheritance relationship, we pay more attention to its generality rather than the characteristic, because generality is the basis of the hierarchy reuse, and the characteristic is the base point of the system expansion.
(3) from the macroscopic point of view, inheritance pays more attention to commonality, while multi-state focuses on difference
(4) The hierarchy of inheritance should be controlled, otherwise the relationship between the types of maintenance will consume more energy
(5) Object-oriented principle: multi-combination, less inheritance; low-coupling, high cohesion

Object-oriented-inheritance (which you must know.) NET reading notes)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.