In this paper, we describe the method of implementing subclass calling Parent class in C #, and share it for everyone's reference. Here's how:
I. Creating a subclass instance from a subclass-free constructor function
Create the parent class person and 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");} }
The child class instance is created on the client through a subclass-free constructor function.
Class program{ static void Main (string[] args) { Student Student = new Student (); Console.readkey (); }}
Output Result:
I'm a man, I'm a student.
Visible: By calling the subclass parameterless constructor to create the subclass instance, the parent class parameterless constructor is called by default.
What happens if we remove the parameterless constructor of the parent class?
– The result will be "the person does not contain 0 parameters of the constructor" error.
Second, the subclass has the parameter constructor to create the sub-class instance
Add a parameter constructor for both the subclass and the parent class.
public class person{Public person () { Console.WriteLine ("I am a person"); } Public person (string name) { Console.WriteLine ("I am human, 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);} }
A subclass instance is created on the client through a subclass with a parameter constructor.
Student Student = new Student ("Xiaoming"); Console.readkey ();
Output Result:
I am a person, I am a student, my name is Xiao Ming
Visible: The parent class parameterless constructor is also called by default by calling the child class with a parameter constructor.
Third, explicitly indicate which parent class constructor is called in the subclass
Above, the non-parametric constructor of the parent class is called by default, but how can I invoke the parameter constructor of the parent class?
– Use base in subclasses
Use base in the parameter constructor in subclass student to explicitly call the parent class with a parameter 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 ("Xiaoming"); Console.readkey ();
Output Result:
I am a person, my name is Xiao Ming I am a student, my name is Xiao Ming
Iv. setting 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 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 human, my name is {0}", name);} }
On the client:
Student Student = new Student ("Xiaoming"); Console.WriteLine ("Subclass Gets the Name property value of the parent class is {0}", student. Name); Console.readkey ();
Output Result:
I am a person, my name is Xiao Ming I am a student, my name is called Ming class gets the Name property value of the parent class for Xiaoming
The execution path for the above code is:
→ Call the subclass with a parameter constructor and pass the parameter to the parent class with the parameter constructor
→ Call the parent class with a parameter constructor and assign a value to the parent class public property name
→ Subclass instances Invoke the public properties of the parent class
In fact, the above approach has been used very well in the layered architecture design. In a hierarchical schema, a base class is typically created for all repository, a property that represents the current repository is designed in the base class, and the property is assigned a value in the constructor of the base class, and finally, when the subclass repository instance is created, in the base class, Represents the Public property assignment for the current repository.
In subclasses, when the parent class gets the parameters of the child class through base, it can also do some processing on the parameter, such as the base of the parent class to convert the parameters 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 Result:
I am a person, my name is Darren I am a student, my name is Darren Subclass gets the Name property value of the parent class is Darren
Summarize:
①.
Creating a subclass instance from a subclass parameterless constructor will call the parent class's parameterless constructor by default
Ii.
A subclass instance is created by a subclass with a parameter constructor, and the non-parametric constructor of the parent class is also called by default
③.
The parent class constructor is indicated by the base keyword in the subclass constructor, and when an instance is created through a subclass constructor, the specified constructor of the parent class is called
④.
The public properties of the parent class can be assigned by a subclass, and the child class can also get the public properties of the parent class
It is believed that through the analysis of the above examples, we can deepen the understanding of the initialization and inheritance of C # class. I hope this article will be helpful for you to further study C # programming.
In addition to the Declaration,
Running GuestArticles are original, reproduced please link to the form of the address of this article
Implementation method of calling parent class in C # Neutron class
This address: http://www.paobuke.com/develop/c-develop/pbk23556.html
Related content C# Édd?? D?3ìdòéè?? ′ú?? The significance of C # Replace in regular expressions common misconceptions of the Equals method in C # methods of sending mail using CDO
C # Image Contrast adjustment Method C # Get real IP Address implementation Method C # using OPENXML to add text to a Word document C # implements a way to control sound playback through Winmm.dll
Implementation method of calling parent class in C # Neutron class