C # How does the subclass call the parent class,

Source: Internet
Author: User

C # How does the subclass call the parent class,

C # How does a subclass call the parent class? With this problem, the experience is as follows:

 

□Create a subclass instance using a subclass without parameters Constructor

Create the parent class Person and the subclass Student.

   public class Person
    {
        public Person()
        {
Console. WriteLine ("I am a person ");
        }
    }
    public class Student : Person
    {
        public Student()
        {
Console. WriteLine ("I am a student ");
        }
    }

 

Create a subclass instance on the client using the subclass without parameters constructor.

    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student();
            Console.ReadKey();
        }
    }

 

Output result:
I am a person
I am a student

It can be seen that by calling a subclass without any parameters constructor to create a subclass instance, the parent class without parameters constructor is called by default.

 

What if I remove the non-argument constructor of the parent class?
-- The error "the Person does not contain 0 parameters of the constructor" is returned.

 

□Create a subclass instance using a subclass constructor with Parameters

Add the constructor with parameters for both the subclass and parent classes.

    public class Person
    {
        public Person()
        {
Console. WriteLine ("I am a person ");
        }
        public Person(string name)
        {
Console. WriteLine ("I am a person, my name is {0}", name );
        }
    }
    public class Student : Person
    {
        public Student()
        {
Console. WriteLine ("I am a student ");
        }
        public Student(string name)
        {
Console. WriteLine ("I am a student, my name is {0}", name );
        }
    }

 

Create a subclass instance on the client using the subclass constructor with parameters.

Student student = new Student ("James ");
 Console.ReadKey();

Output result:
I am a person
I am a student. My name is James.

 

It can be seen that by calling the sub-class constructor with parameters, the parent class constructor is also called by default.

 

□Specify the parent class constructor to be called in the subclass.

In the preceding example, the non-argument constructor of the parent class is called by default. But how can I call the parametric constructor of the parent class?
-- Use base in subclass

 

Use base in the parameter constructor of the Student subclass to explicitly call the parameter constructor of the parent class.

    public class Student : Person
    {
        public Student()
        {
Console. WriteLine ("I am a student ");
        }
        public Student(string name)
            : base(name)
        {
Console. WriteLine ("I am a student, my name is {0}", name );
        }
    }

 

Client

Student student = new Student ("James ");
 Console.ReadKey();

 

Output result:
I am a human, and my name is James
I am a student. My name is James.

 

□Set the public attributes of the parent class through subclass

Add a Name public attribute to the parent class Person and assign a value to the Name attribute in the constructor of the parent class.

    public class Person
    {
        public string Name { get; set; }
        public Person()
        {
Console. WriteLine ("I am a person ");
        }
        public Person(string name)
        {
            this.Name = name;
Console. WriteLine ("I am a person, my name is {0}", name );
        }
    }

 

 

On the client:

Student student = new Student ("James ");
Console. WriteLine ("The subclass obtains the Name attribute value of the parent class as {0}", student. Name );
Console.ReadKey();    

 

Output result:
I am a human, and my name is James
I am a student. My name is James.
The subclass obtains the Name attribute value of the parent class as Xiaoming.

 

The execution path of the above Code is:
→ Call the sub-class constructor with parameters and pass the parameter value to the parent class constructor with parameters.
→ Call the constructor with parameters for the parent class and assign a value to the Name of the common attribute of the parent class
→ The subclass instance calls the public attributes of the parent class

 

In fact, the above practices have been well used in the layered architecture design. In a layered architecture, a base class is usually created for all Repository. In the base class, an attribute representing the current Repository is designed and assigned to the Base Class constructor; finally, when creating a subclass Repository instance, assign values to the public attributes of the base class that represent the current Repository.

 

In a subclass, when the parent class obtains the parameter of the subclass through the base class, it can also perform some processing on this generation. For example, the base representing the parent class converts the parameter obtained from the subclass into a large write.

     public class Student : Person
    {
        public Student()
        {
Console. WriteLine ("I am a student ");
        }
        public Student(string name)
            : base(ConvertToUpper(name))
        {
Console. WriteLine ("I am a student, my name is {0}", name );
        }
        private static string ConvertToUpper(string name)
        {
            return name.ToUpper();
        }
    }

 

Output result:
I am a human, and my name is DARREN.
I'm a student. My name is darren.
Subclass obtains the Name attribute of the parent class as DARREN.

 

Summary:
● If you create a subclass instance by using a subclass without any parameters constructor, the non-argument constructor of the parent class is called by default.
● Create a subclass instance using a subclass constructor with parameters. By default, the non-argument constructor of the parent class is also called.
● Specify the parent class constructor using the base keyword in the subclass constructor. When an instance is created using the subclass constructor, the specified parent class constructor is called.
● The Public attributes of the parent class can be assigned values through the subclass, And the subclass can also obtain the public attributes of the parent class.


In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

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.