Like the this keyword, It is abbreviated or replaced as an instance of the class (so it cannot call static and abstract members of the base class, however, this keyword is used to replace the instance of this class. The base keyword is used to replace the instance of the base class. Its usage is very simple. The form of accessing the base class is as follows:
Base. [identifier]
Base [[expression list] you can probably guess at a glance that it is mostly used for base-class instance index operations. You will see its usage in the code I demonstrated below.
The ACCESS format of base. [identifier] is described as follows:
For non-virtual methods, such access is only a direct access to the base class instance members, which is equivalent to (base) This). [identifier ].
For a virtual method, this method is used to override the virtual method for this type of access subclass (the virtual method call mechanism is disabled) for direct access to members of the base class instance, it is treated as a non-virtual method, which is not equivalent to (base) This ). [identifier], because this format fully complies with the virtual method call mechanism, its declaration is accumulation type, Runtime is subclass type, so the execution is a subclass rewrite method. The unoverwritten virtual method is equivalent to a simple non-virtual method.
The test code is as follows:
Using system;
Namespace basetest
{
Class father
{
String str1 = "this field [1] of baseclass", str2 = "this field [2] of baseclass ";
Public void F1 () // non-virtual Method
{
Console. writeline ("F1 of the baseclass ");
}
Public Virtual void F2 () // virtual Method
{
Console. writeline ("F2 of the baseclass ");
}
Public Virtual void F3 ()
{
Console. writeline ("F3 of the baseclass that is not overrided ");
}
Public String This [int Index]
{
Set
{
If (Index = 1)
{
Str1 = value;
}
Else
{
Str2 = value;
}
}
Get
{
If (Index = 1)
{
Return str1;
}
Else
{
Return str2;
}
}
}
}
Class child: Father
{
Public void g ()
{
Console. writeline ("======= non-Virtual Methods Test ========= ");
Base. F1 ();
(Father) This). F1 ();
Console. writeline ("===== Virtual Methods Test ========= ");
Base. F2 ();
(Father) This). F2 ();
Base. F3 ();
(Father) This). F3 ();
Console. writeline ("===== test the type that the tbase [[expression] ========== ");
Console. writeline (base [1]);
Base [1] = "override the default ";
Console. writeline (base [1]);
Console. writeline ("======================== test over ================================= = ");
}
Public override void F2 ()
{
Console. writeline ("F2 of the subclass ");
}
Static void main (string [] ARGs)
{
Child child = new child ();
Child. G ();
Console. readkey ();
}
}
}
Base is used for Constructor declaration. Its usage is exactly the same as this for Constructor declaration. However, base is a match for the base class constructor parameters.
Using system;
Namespace basecotest
{
Class base
{
Public base (int A, string Str)
{
Console. writeline ("base. Base (int A, string Str )");
}
Public base (int)
{
Console. writeline ("base. Base (int )");
}
Public base ()
{
}
}
Class Sub: Base
{
Public sub ()
{
}
Public sub (int)
: Base (1, "123 ")
{
Console. writeline ("sub. sub (int )");
}
Class Test
{
Public static void main ()
{
Sub = new sub (1 );
Console. readkey ();
}
}
}
}