C # some confusing concepts (v) --------- in-depth analysis of C # inheritance

Source: Internet
Author: User

The main content of this article is about inheritance. First, let's take a look at inheritance. Since there is inheritance, there must be a parent class and a subclass. Let's look at the following code: copy the code class Person {private int nAge; protected string strName; double douHeight; public string strEateType; public void Hello () {Console. writeLine ("I can say Hello! ");} Public void Run () {Console. WriteLine (" I can Run! ") ;}} Class Student: Person {} copy the code and I instantiate the subclass object in the Main () function. The Code is as follows: static void Main (string [] args) {Student stu1 = new Student ();} So what happens in the memory during this process? Let's first look at misl's intermediate code to see what we can find. From this we can find that the subclass inherits all the members of the parent class, including Private and Protect, and opened up space for these Members to store. Let's instantiate our subclass and then access the fields and methods of the parent class. We will find that the following phenomena exist. Although the subclass opens up space for all the members of the parent class in the heap, however, Private and protected members cannot access the service. So in the memory, for example, look at the following code. Let's explore the members of the class accessed by the this keyword and the base keyword in the subclass. The Code is as follows: copy the code class Student: Person {private string strClass; private string strAddress; public void Address (string linoleic, string adre) {// here, the this keyword calls the member of the subclass and the non-seemingly Member of the parent class this. strClass = "5"; this. strAddress = "Beijing"; this. strName = "Zi Qiang"; // here, the base keyword is called as a non-seemingly member base of the parent class. strName = "Hadron"; Console. writeLine ("I'm {0} years old, from {1}", linoleic, adre);} public void Sing () {This. strClass = ""; Console. WriteLine ("I can sing! ") ;}} Copy the code so the access range of the this keyword and the base keyword in the subclass is as follows: 2, for the execution sequence of the constructor and parent class constructor of the subclass object, we add an explicit constructor for the parent class and the subclass respectively. The code below copies the code class Person {private int nAge; protected string strName; double douHeight; public string strEateType; // constructor of the parent class public Person () {Console. writeLine ("I am the constructor of the parent class");} public void Hello () {Console. writeLine ("I can say Hello! ");} Public void Run () {Console. WriteLine (" I can Run! ") ;}} Class Student: Person {private string strClass; private string strAddress; // constructor of the subclass public Student () {Console. writeLine ("I Am a subclass constructor");} copy the code. We use the one-step debugging of VS to see the execution sequence of the parent class and subclass explicitly constructor, for example, you can see the process of dynamic images, when creating a subclass object, ① first calls the constructor of the subclass 2 calls the constructor of the parent class 3 executes the constructor of the parent class 4 executes the constructor of the subclass. Why? so what? I tried to look at the source code through decompilation to explain this reason, but the results of decompilation are as follows. I didn't find anything special to explain this reason. Finally, I found the answer in Microsoft's official MSDN document (Click here for the original address). Based on the official code example of Microsoft, then, the following code will have the same effect as copying the code // The constructor public Student () {Console. writeLine ("I Am a subclass of the constructor");} // The code here has the same effect as the above Code: public Student (): base () {Console. writeLine ("I Am a subclass constructor");} copy the code to explicitly declare a non-argument constructor In the subclass, the non-argument constructor of the subclass calls the constructor without parameters of the parent class. If the parent class does not have this non-argument constructor, an error is returned. The following code: copy the code class Person {private int nAge; protected string strName; double douHeight; public string strEateType; // constructor of the parent class // public Person () // {// Console. writeLine ("I am the constructor of the parent class"); // The constructor with parameters for the parent class. The constructor without parameters is public Person (string str) {Console. writeLine ("I am the constructor of the parent class {0}", str);} public void Hello () {Console. writeLine ("I can say Hello! ");} Public void Run () {Console. WriteLine (" I can Run! ") ;}} Class Student: Person {private string strClass; private string strAddress; // public Student () {Console. writeLine ("I Am a subclass constructor");} public Student (string strName) {Console. writeLine ("My name is {0}", strName) ;}} when copying the code, an error is reported during compilation because the constructor with parameters in the parent class overwrites the non-parameter constructor, therefore, the non-parameter constructor of the subclass cannot call back the non-parameter constructor of the parent class to initialize the member variables of the parent class. So an error is reported. So why should I call the constructor of the parent class when initializing the subclass? Before initializing a subclass, You need to initialize the member variable of the parent class through the constructor. What is the significance of the constructor of the parent class before the constructor of the subclass is executed? Assign different default values to non-private member variables of the parent class in the constructor of the parent class and the constructor of the Child class. When instantiating a subclass, The subclass calls the constructor to initialize the member variable. If the constructor of the subclass is executed first, the constructor of the parent class is executed, the value of the parent class member field overwrites the value of the Child class member field. But what we want is the property value of the subclass. To resolve data conflicts, the constructor of the parent class must be executed before the constructor of the Child class. The following code: copy the code class Person {private int nAge; private string strName; double douHeight; public string strEateType; // The constructor of the parent class public Person () {// then assign the initial value this to strEateType in the parent class. strEateType = "dinner"; Console. writeLine ("I am the constructor of the parent class {0}", strEateType) ;}} class Student: Person {private string strClass; private string strAddress; // The subclass constructor public Student () {// assign the initial value this to the strEateType In the subclass. strEateType = "noodles"; Co Nsole. writeLine ("I'm a subclass of the constructor {0}", strEateType) ;}} copy the Code. At this time, we use the statement to declare that the subclass object accesses the value of strEateType, as shown below: student stu1 = new Student (); // stu1. string str = stu1.strEateType. toString (); Console. writeLine (str); Console. readKey (); the value of the strEateType attribute of the subclass must be printed here. If you first run the subclass constructor to assign a value to the strEateType, then the constructor value of the parent class overwrites the initial value of the strEateType. The printed value is the value of the parent class member field. Therefore, the constructor of the parent class is executed before the constructor of the Child class. The printed result is as follows: 3. Can the subclass have the same name as the parent class? Check the following code. We declare a parent class Person: copy the code class Person {private int nAge; private string strName; double douHeight; public string strEateType; public readonly string strrrr; // constructor of the parent class public Person () {this. strEateType = "dinner"; Console. writeLine ("I am the constructor of the parent class {0}", strEateType);} public Person (string str) {this. strName = str; Console. writeLine ("I am the constructor of the parent class {0}", str);} public void Hello () {Console. WriteLine ("I can say Hello from Earth! ");} Public void Run () {Console. WriteLine (" I can Run! ") ;}} Copy the code to declare that a subclass inherits Person. The Code is as follows: copy the code class Worker: Person {public void Hello () {Console. writeLine ("I am a worker and will say Hello! ");} Public new void Run () {Console. WriteLine (" I am a worker and I will Run! ");}}

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.