Base
The base keyword is used to access the members of the base class from the derived class:
Call methods that have 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.
// Base keyword
// Access Base Class Members
Using system;
Public class baseclass
{
Protected string _ classname = "baseclass ";
Public Virtual void printname ()
{
Console. writeline ("Class Name: {0}", _ classname );
}
}
Class derivedclass: baseclass
{
Public String _ classname = "derivedclass ";
Public override void printname ()
{
Console. Write ("The baseclass name is {0 }");
// Call the base class Method
Base. printname ();
Console. writeline ("This derivedclass is {0}", _ classname );
}
}
Class testapp
{
Public static void main ()
{
Derivedclass Dc = new derivedclass ();
DC. printname ();
}
}
/**//*
Console output:
The baseclass name is baseclass
This derivedclass is derivedclass
*/
Call the base class constructor In the derived class.
// Keywords_base2.cs
Using system;
Public class baseclass
{
Int num;
Public baseclass ()
{
Console. writeline ("in baseclass ()");
}
Public baseclass (int I)
{
Num = I;
Console. writeline ("in baseclass (INT {0})", num );
}
}
Public class derivedclass: baseclass
{
// The constructor calls baseclass. baseclass ()
Public derivedclass (): Base ()
{
}
// The constructor calls baseclass. baseclass (int I)
Public derivedclass (int I): Base (I)
{
}
Static void main ()
{
Derivedclass Dc = new derivedclass ();
Derivedclass DC1 = new derivedclass (1 );
}
}
/**//*
Console output:
In baseclass ()
In baseclass (1)
*/
Notes
It is incorrect to use the base keyword in a static method.
Base is mainly used for object-oriented development in this aspect, which is embodied in example 2.
This
This keyword references the current instance of the class.
The following are common uses of this:
Restrict hidden members with similar names
Passing objects as parameters to other methods
Declare Indexer
Example:
Comprehensive example.
// This keyword
// Keywords_this.cs
Using system;
Class employee
{
Private string _ name;
Private int _ age;
Private string [] _ arr = new string [5];
Public Employee (string name, int age)
{
// Use this to specify the field, name and age.
This. _ name = Name;
This. _ age = age;
}
Public string name
{
Get {return this. _ name ;}
}
Public int age
{
Get {return this. _ age ;}
}
// Print employee information
Public void printemployee ()
{
// Pass the employee object as a parameter to the doprint Method
Print. doprint (this );
}
// Declare the Indexer
Public String This [int Param]
{
Get {return _ arr [Param];}
Set {_ arr [Param] = value ;}
}
}
Class print
{
Public static void doprint (employee e)
{
Console. writeline ("Name: {0} \ Nage: {1}", E. Name, E. Age );
}
}
Class testapp
{
Static void main ()
{
Employee E = new employee ("hunts", 21 );
E [0] = "Scott ";
E [1] = "Leigh ";
E [4] = "kiwis ";
E. printemployee ();
For (INT I = 0; I <5; I ++)
{
Console. writeline ("friends name: {0}", E [I]);
}
Console. Readline ();
}
}
/** // *
console output:
name: hunts
Age: 21
friends name: Scott
friends name: whale
friends name: kiwis
*/