Copy Code code as follows:
Class A
{
public virtual void Func ()//Note virtual, which indicates that this is a dummy function
{
Console.WriteLine ("Func in A");
}
}
Class B:a//note B is inherited from Class A, so a is a parent class and B is a subclass
{
public override void Func ()//note override, indicating that virtual functions have been implemented again
{
Console.WriteLine ("Func in B");
}
}
Class C:b//Note C is inherited from Class B, so B is the parent class and C is a subclass
{
}
Class D:a//Note D is inherited from Class A, so a is a parent class and D is a subclass
{
Public new void Func ()//note new, which means overriding the class of the same name in the parent class, rather than implementing the
{
Console.WriteLine ("Func in D");
}
}
Class Program
{
static void Main (string[] args)
{
A A; An object that defines a class A. This is a declaration class for a.
A b; Defines the object of a B-Class A. This is the declaration class of B.
A C; Defines a C this object of Class A. This is a C declaration class.
A D; Defines a Class D object. This is the declaration class for D.
A = new A (); Instantiate a object, a is an instance class of a
b = new B (); Instantiate B object, B is instance class of B
c = new C (); Instantiate C object, C is instance class of C
D = new D (); Instantiate D object, D is instance class of D
A.func (); Execute a. Func:1. Check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class A, for itself 4. Execute method 5 of instance Class A. Output Func in a
B.func (); Execution B. Func:1. Check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class B, with overloaded 4. Execute method 5 in instance Class B. Output Func in B
C.func (); Execute c. Func:1. Check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class C, no overload 4. Go to check Class C's parent Class B, with overloaded 5. Func method in parent Class B 5. Output results Func in B
D.func (); Execute d. Func:1. First check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class D, no overload (this place should be noted, although D has implementation Func (), but does not use the Override keyword, so will not be considered an overload) 4. Go to check Class D's parent A, for itself 5. Executes the Func method in the parent class A 5. Output result Func in a
D d1 = new D ();
D1. Func (); Executes the Func () in Class D, and outputs the result Func in D
Console.ReadLine ();
}
}