[C#] In C#, the subclass calls the implementation method of the parent class

The example of this article describes the method of implementing a subclass to call the parent class in C#, and shares it with you for your reference. The specific method is as follows:

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

The subclass instance is created on the client side through the subclass no-argument constructor.

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

i am a student

Visible: Create a subclass instance by calling the subclass no-argument constructor, and the parent class's no-argument constructor will be called by default.
What happens if the no-argument constructor of the parent class is removed?

-- The result will report "Person does not contain a constructor with 0 parameters" error.

2. Create a subclass instance through the subclass parameterized constructor

Add parameter constructors for both the subclass and the superclass at the same time.

public class Person
{
    public Person()
    {
      Console.WriteLine("I am human");
    }
    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);
    }
}

The subclass instance is created on the client side through the subclass parameterized constructor.

Student student = new Student("小明");
Console.ReadKey();

Output result: Output result:
I am human

I'm a student, my name is Xiaoming

Visible: By calling the subclass parameterized constructor, the parent class parameterless constructor will also be called by default.

3. Clearly indicate which parent class constructor to call in the subclass

Above, the no-argument constructor of the parent class is called by default, but how to call the parameter-less constructor of the parent class?
-- use base in subclasses

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


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("小明");
Console.ReadKey();
Output result:
I am a human, my name is Xiaoming

I'm a student, my name is Xiaoming

Fourth, set the public properties of the parent class through the subclass

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


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

On the client side:

Student student = new Student("小明");
Console.WriteLine("Subclass gets the Name property value of parent class as {0}", student.Name);
Console.ReadKey();  

Output result:

I am a person, my name is Xiaoming
, I am a student, my name is Xiaoming 

The subclass gets the Name property value of the parent class as Xiaoming

The execution path of the above code is:

→Call the constructor with parameters of the subclass, and pass the parameter value to the constructor with parameters of the parent class
→Call the constructor with parameters of the parent class, and assign a value to the public property Name of the parent class
→The instance of the subclass calls the public properties of the parent class

In fact, the above approach has been well used in the design of layered architecture. In a hierarchical architecture, a base class is usually created for all Repositories, an attribute representing the current Repository is designed in the base class, and the attribute is assigned a value in the constructor of the base class; finally, the subclass Repository instance is created When assigning values ​​to the public properties of the base class that represent the current Repository.

In the subclass, when the parent class gets the parameter of the subclass through base, it can also do some processing on the parameter, for example, the base representing the parent class converts the parameter obtained from the subclass to uppercase.


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:
I am a person, my name is DARREN
, I am a student, my name is darren

The subclass gets the Name property value of the parent class as DARREN

Summarize:

①. To create a subclass instance through the subclass no-parameter constructor, the no-parameter constructor of the parent class will be called by default
. ②. To create a subclass instance through the subclass's parameterless constructor, the parent class's no-parameter constructor will also be called by default.
③ .In the subclass constructor, specify the parent class constructor through the base keyword. When an instance is created through the subclass constructor, the specified constructor of the parent class will be called
. ④. The public properties of the parent class can be assigned through the subclass, Subclasses can also get the public properties of the parent class


Original address: http://www.jb51.net/article/54779.htm

?
?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326212428&siteId=291194637