Example:
using System;using System.Collections.Generic;using System.Linq;using System.Text; public class BaseClass { public virtual void displayName() { Console.WriteLine("BaseClass"); } } public class DerivedClass : BaseClass { public virtual void displayName() { Console.WriteLine("DerivedClass"); } } public class SubDerivedClass:DerivedClass { public override void displayName() { Console.WriteLine("SubDerivedClass"); } } public class SuperDerivedClass:SubDerivedClass { public void displayName() { Console.WriteLine("SuperDerivedClass"); } } class Program { static void Main(string[] args) { SuperDerivedClass superDerivedClass = new SuperDerivedClass(); SubDerivedClass subDerivedClass = superDerivedClass; DerivedClass derivedClass = superDerivedClass; BaseClass baseClass = superDerivedClass; superDerivedClass.displayName(); subDerivedClass.displayName(); derivedClass.displayName(); baseClass.displayName(); } }
Execution result
Result Analysis:
SuperDerivedClass. displayName ();
No need to explain. No subclass, so no polymorphism.
SubDerivedClass. displayName ();
SubDerivedClass overrides the DerivedClass method, and displayName in subDerivedClass is also a virtual method. Therefore, when running, try to find the farthest virtual method derived from the inheritance chain, and find that its subclass SuperDerivedClass does not use override, this means that SuperDerivedClass only overwrites the displayName method and does not overwrite it. Therefore, the displayName of the SuperDerivedClass is not executed, but the displayName of the subDerivedClass is executed.
DerivedClass. displayName ();
DerivedClass overwrites (overwrites, not overwrites, because there is no override label) The BaseClass method, but the overwrites method is declared as virtual, therefore, when running, we should try to find the farthest virtual method derived from the inheritance chain and find that its SubDerivedClass sub-class overwrites this method. So we can continue to find the SubDerivedClass and find the SuperDerivedClass, it is found that SuperDerivedClass overwrites this method and is not overwritten. Therefore, the SubDerivedClass displayName method is finally executed.
BaseClass. displayName ();
DisplayName is a virtual method in BaseClass, so it tries to find the farthest derived virtual method. But when we find DerivedClass, we find that DerivedClass overwrites displayName, and this virtual link is broken. Therefore, you can directly execute the BaseClass method.
Note:
If the override of SubDerivedClass is removed, it is not possible to Write public override void displayName () in SuperDerivedClass. Although there is a virtual displayName in DerivedClass for the subclass override, The DerivedClass in SubDerivedClass does not include override, which is equivalent to SubDerivedClass covering the displayName method of DerivedClass, rather than override, the displayName seen in SuperDerivedClass is actually the dislayName of SubDerivedClass. It is a non-virtual method instead of a virtual method inherited from DerivedClass.
Summary
If a class has one method, the method is a virtual method only in two cases.
1: This method is defined by virtual.
2: There is a virtual method with the same name in the parent class of this class. In this class, the override keyword is used to override the virtual method of the parent class.
To "Override" a subclass rather than "Overwrite" A parent class, two conditions must be met.
1. "The parent method is a virtual method. (This means that this method is either marked as virtual, or override its parent class .)".
2. "The subclass has the override mark ".
If either of them is missing, it means that the subclass overwrites the method of the parent class, rather than overwrites the method ".
If a method is a virtual method (), it is executed by looking for the "Override" method in the runtime. Once "Overwrite" is encountered, the inheritance chain is broken.