This article describes the method of implementing subclass calls to parent classes in C#, and is shared with you for your reference. The specific methods are as follows:
1. Create a subclass instance through the subclass parameterless constructor
Create parent class Person and child class Student.
public class Person { public Person() { ("I am a human"); } } public class Student : Person { public Student() { ("I am a student"); } }
Create a subclass instance on the client through the subclass parameterless constructor.
class Program { static void Main(string[] args) { Student student = new Student(); (); } }
Output result:
I'm human I'm a student
It can be seen that by calling the subclass parameterless constructor to create a subclass instance, the parent class parameterless constructor will be called by default.
What if the parameterless constructor of the parent class is removed?
--The result will report "Person does not contain 0 parameters" error.
2. Create subclass instances through subclass parameter constructor
Then add parameter constructors to both the subclass and the parent class.
public class Person { public Person() { ("I am a human"); } public Person(string name) { ("I'm human,My name is{0}", name); } } public class Student : Person { public Student() { ("I am a student"); } public Student(string name) { ("I'm a student,My name is{0}", name); } }
Create a subclass instance on the client through the subclass parameter constructor.
Student student = new Student("Xiao Ming"); ();
Output result:
I'm human I'm a student,My name is Xiao Ming
It can be seen that by calling the parameter constructor of the subclass, the parent class without parameter constructor will also be called by default.
3. To specify which parent class constructor is called in the subclass
The above is called by default the parameterless constructor of the parent class, but how to call the parameterless constructor of the parent class?
--Use base in subclasses
Use base in the parameter constructor in the subclass Student to explicitly call the parameter constructor of the parent class.
public class Student : Person { public Student() { ("I am a student"); } public Student(string name) : base(name) { ("I'm a student,My name is{0}", name); } }
Client
Student student = new Student("Xiao Ming"); ();
Output result:
I'm human,My name is Xiao Ming I'm a student,My name is Xiao Ming
4. Set the public properties of the parent class through subclasses
Add a Name public property to the parent class Person and assign a value to the Name property in the parent class constructor.
public class Person { public string Name { get; set; } public Person() { ("I am a human"); } public Person(string name) { = name; ("I'm human,My name is{0}", name); } }
On the client:
Student student = new Student("Xiao Ming"); ("Subclass gets the parent classNameThe attribute value is{0}", ); ();
Output result:
I'm human,My name is Xiao Ming I'm a student,My name is Xiao Ming Subclass gets the parent classNameThe attribute value is Xiao Ming
The execution path of the above code is:
→Call the parameter constructor of the subclass and pass the parameter value to the parent class parameter constructor
→Call the parameter constructor of the parent class and assign a value to the public property Name of the parent class
→Subclass instance calls the public property of the parent class
In fact, the above practices have been well used in hierarchical architecture design. In a hierarchical architecture, a base class is usually created for all Repository, and an attribute representing the current Repository is designed in the base class, and a value is assigned to the attribute in the constructor of the base class; finally, when creating a subclass Repository instance, a value is assigned to the public attribute of the base class that represents the current Repository.
In a subclass, when the parent class gets the parameters of the subclass through the base, it can also do some processing on the parameter, such as converting the parameters obtained from the subclass into capitalization based on the base representing the parent class.
public class Student : Person { public Student() { ("I am a student"); } public Student(string name) : base(ConvertToUpper(name)) { ("I'm a student,My name is{0}", name); } private static string ConvertToUpper(string name) { return (); } }
Output result:
I'm human,My name isDARREN I'm a student,My name isdarren Subclass gets the parent classNameThe attribute value isDARREN
Summarize:
①.Create a subclass instance through the subclass without parameter constructor, and the parent class's parameterless constructor will be called by default
②.Create a subclass instance through a subclass parameter constructor, and the parent class's parameterless constructor will also be called by default.
③.In the subclass constructor, the parent class constructor is indicated through the base keyword. When an instance is created through the subclass constructor, the specified parent class constructor will be called
④.The public attributes of the parent class can be assigned through subclasses, and the subclass can also obtain the public attributes of the parent class.
I believe that through the analysis of the above examples in this article, everyone's understanding of the initialization and inheritance of C# classes can be deepened. I hope that this article will be helpful to you in further learning C# programming.