Inherit the essence of personal learning notes, inherit the essence of learning notes

Source: Internet
Author: User

Inherit the essence of personal learning notes, inherit the essence of learning notes

I have read some articles and blogs about the nature of inheritance and summarized it into my own words to deepen my understanding. It may not be very thorough in expression and understanding. Please read it with a critical eye ~
First, I will attach the articles of the great gods ~

Http://www.cnblogs.com/pbwf/archive/2008/03/27/1125451.html


When new creates an object, recursively searches for the parent class and allocates memory. The first object to be created should be an object.
An object is created at runtime. The method is created at compilation. The creation and calling of object fields and methods follow the principle of proximity. Therefore, subclass copies the methods and fields of the parent class, the fields and methods of the parent class are in front of each other. The new methods and fields added to the subclass are called in the order of fields and method storage. If the subclass overrides the methods of the parent class, the method copied from the parent class will be overwritten. If the new method is used to define the same name, the implementation of the parent class method will be hidden. The method of the parent class still exists, before the method of the subclass with the same name
The called method is of the subclass or parent class and has nothing to do with the reference type. It is only related to whether the created object is a subclass or a parent class Object (focus on the object principle ), at the same time, declaring different reference types only determines the different access permissions for methods. The parent class can declare the creation of subclass objects because the subclass inherits the methods and fields of the parent class, in a subclass, a reference of the parent type has access permissions. Subclass objects cannot be declared and created because subclass pointers in the parent class do not have access permissions.
The reference of the parent type can only call the methods and fields copied from the parent class in the subclass. The newly added methods and fields of the subclass cannot be called. The priority of a child-type reference call is based on the parent class and the Child class. If the child class uses new to define a method with the same name, the method of the parent class is hidden and the Child class method is preferentially called.


Simple code to help you understand:
Public class Bird
{
Private string type = "bird ";
Public virtual string ShowType ()
{
Return "bird ";
}
}

Public class Chicken: Bird
{
Private new string type = "chicken ";
Public string show ()
{
Return "chicken ";
}
Public override string ShowType ()
{
Return "chicken ";
}
}
The call code is as follows:
Bird bird = new Chicken ();
Chicken chicken = new Chicken ();
String str1 = bird. ShowType ();
String str2 = chicken. ShowType ();
Results: str1-chicken str2-chicken

Change the Class Method
Public class Chicken: Bird
{
Private new string type = "chicken ";
Public string show ()
{
Return "chicken ";
}
Public new string ShowType ()
{
Return "chicken ";
}
}
Results: str1-bird str2-chicken
Although all the chicken objects are created, the bird cannot call the show () method in the chicken, and the chicken method is acceptable, which means the access permissions of the reference type are different.

Related Article

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.