The subclasses in C call the implementation methods of the superclass

  • 2020-10-23 21:11:08
  • OfStack

This article describes the example of C# to implement the subclass to call the superclass method, to share for your reference. The specific methods are as follows:

1. Create a subclass instance with a subclass-free constructor

Create the parent Person and the child Student.


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

A subclass instance is created on the client side through a subclassless constructor.


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

Output results:


 I am a man 
 I am a student 

You can see that creating a subclass instance by calling a subclass parameter-free constructor will call the superclass parameter-free constructor by default.

What happens if you remove the superclass's no-argument constructor?
"Person constructor with no 0 arguments "error.

2. Create a subclass instance with a subclass parameter constructor

Add parameter constructors for both subclasses and superclasses.


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

A subclass instance is created on the client side with a subclass argument constructor.


Student student = new Student(" Xiao Ming ");
Console.ReadKey();

Output results:


 I am a man 
 I'm a student. My name is Xiao Ming 

Visible: By calling a subclass with parameter constructor, the superclass without parameter constructor is also called by default.

3. Specify in subclasses which superclass constructor to call

Above, the superclass's no-parameter constructor is called by default, but how do you call the superclass's parameter-constructor?
-- Use base in subclasses

Use base in the parameter constructor in the subclass Student to explicitly call the superclass parameter constructor.


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

The client


Student student = new Student(" Xiao Ming ");
Console.ReadKey();

Output results:


 I am a man. My name is Xiaoming 
 I'm a student. My name is Xiao Ming 

4. Set the public properties of the superclass by subclasses

Add one Name common attribute to the parent class Person and assign the Name attribute to the constructor of the parent class.


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

On the client side:


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

0

Output results:


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

1

The execution path of the above code is:

→ Call the subclass parameter constructor and pass the parameter value to the superclass parameter constructor
→ Calls the superclass parameter constructor and assigns the superclass common property Name
→ The subclass instance calls the public property of the superclass

In fact, the above approach has been well used in hierarchical architecture design. In the hierarchical architecture, one base class is usually created for all Repository, and an attribute representing the current Repository is designed in the base class, and the attribute is assigned in the constructor of the base class. Finally, when you create an instance of the subclass Repository, assign a value to the public property of the base class that represents the current Repository.

In a subclass, when the parent gets the subclass's argument through base, there is also something to be done with the argument, such as base, which represents the parent, converting the argument taken from the subclass to uppercase.


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

2

Output results:


 I'm a man. my name is DARREN
 I'm a student. My name is darren
 A subclass gets the superclass Name Attribute values for DARREN

Conclusion:

. Creating a subclass instance by subclass-free constructor will call the superclass's parameter-free constructor by default
(2). Create a subclass instance by subclassed with parameter constructor, but also call the superclass without parameter constructor by default
In the subclass constructor, the superclass constructor is specified by the base keyword. When an instance is created by the subclass constructor, the specified superclass constructor is called
The public attribute of the superclass can be assigned by the subclass, the subclass can also get the public attribute of the superclass

It is believed that through the analysis of the above examples in this article, we can deepen our understanding of the initialization and inheritance of C# class. I hope this article has been helpful for you to learn C# programming in step 1.


Related articles: