① calls a method on the base class that has been overridden by another method, and the small chestnut A is as follows:
Public classFather { Public Virtual voidShow () {Console.WriteLine ("I am Your Father"); } } Public classChild:father { Public Override voidShow () {Base. Show (); Console.WriteLine ("I am your child"); } }
Operation Result:
② when creating an instance of a derived class, specify the constructor of the base class that is called
Public classFather { Public stringName {Get;Set; } PublicFather (stringName) { This. Name =Name; } } Public classChild:father { PublicChild ():Base("Xiao Ming") {Console.WriteLine ("output base class name:{0}", Name); } } Static voidMain (string[] args) { Child child=NewChild (); Console.readkey (); }
Operation Result:
When a subclass is constructed, the constructor of the parent class is called first, but you can choose which constructor to call with the base keyword, but not each (at least one). If you do not specify it, you typically call the parameterless constructor, because this is the default constructor for a class.
The role of the keyword base