C #KeywordsBase and this. The base keyword is used to access a base class member from a derived class: it calls a method that has been overwritten by other methods on the base class.
Specify the base class constructor to call when creating a derived class instance.
Base Class access can only be performed in constructors, instance methods, or instance attribute accessors.
Example:
Call the base class method in the derived class.
The following is a reference snippet: <br/> // base keyword <br/> // access base class member <br/> using system; <br/> public class baseclass <br/> {<br/> protected string _ classname = "baseclass"; <br/> Public Virtual void printname () <br/> {<br/> console. writeline ("Class Name: {0}", _ classname); <br/>}< br/> class derivedclass: baseclass <br/>{< br/> Public String _ classname = "derivedclass"; <br/> Public override void printname () <br/>{< br/> console. write ("The baseclass name is {0}"); <br/> // call the base class method <br/> base. printname (); <br/> console. writeline ("This derivedclass is {0}", _ classname ); <br/>}< br/> class testapp <br/>{< br/> Public static void main () <br/>{< br/> derivedclass Dc = new derivedclass (); <br/> DC. printname (); <br/>}< br/> /**//*
Console output:
The baseclass name is baseclass
This derivedclass is derivedclass
*/
Call the base class constructor In the derived class.
<Br/> // keywords_base2.cs <br/> using system; <br/> public class baseclass <br/>{< br/> int num; <br/> Public baseclass () <br/> {<br/> console. writeline ("in baseclass ()"); <br/>}< br/> Public baseclass (int I) <br/>{< br/> num = I; <br/> console. writeline ("in baseclass (INT {0})", num); <br/>}< br/> public class derivedclass: baseclass <br/>{< br/> // The constructor calls baseclass. baseclass () <br/> Public derivedclass (): Base () <br/>{< br/>}< br/> // The constructor calls baseclass. baseclass (int I) <br/> Public derivedclass (int I): Base (I) <br/>{< br/>}< br/> static void main () <br/>{< br/> derivedclass Dc = new derivedclass (); <br/> derivedclass DC1 = new derivedclass (1 ); <br/>}< br/> /**//*
Console output:
In baseclass ()
In baseclass (1)
*/
Note:
It is incorrect to use the base keyword in a static method.
BaseIt is mainly used for the polymorphism of object-oriented development, which is embodied in example 2.
ThisKeyword to reference the current instance of the class.
The blog address of the link is: http://blog.163.com/huang_ying_lu/blog/static/269998320081109212363/