If the declaration of an instance method contains a Virtual modifier, the method is called a virtual method. If there is no Virtual modifier, the method is called a non-virtual method.
The implementation of the non-virtual method does not change: Whether in the instance of declaring its class, the method is called on the instance of the derived class, and the implementation is the same. In contrast, the implementation of a virtual method can be replaced by a derived class. The process of replacing the implementation of the inherited virtual method is referred to as rewriting the method (Section 10.5.4).
In a virtual method call, the runtime type of the instance involved in this call determines which implementation of the method to be called. In the non-virtual method call, the compilation type of the relevant instance is a decisive factor. Quickly said that when the parameter list A is called N-N method when there is an instance of the type C and runtime type R (where R is C or class from the C-derived class), the call is processed by the following rules:
First, the overload decision is applied to C, N, and A to select a specific method M from the method declared in C and inherited by C. Section 7.5.5.1 describes this. Then, if m is a non-virtual method, call M. Otherwise (m is a virtual method), the implementation of the degree of derived degree of M in R is called.
For each virtual method declared in a class or by inheritance, there is a maximum degree of derived degree of derivation in this class. The maximum implementation of the virtual method M in the class R is determined by the following rules:
If R is included with the Virtual statement about M, this is the maximum degree of derived degree of M. Otherwise, if R is contained with an Override declaration of M, this is the maximum degree of derived degree of M. Otherwise, the maximum degree of derived degree of M in R is the same as the degree of derived degree of m in the direct base class of R.
The following examples illustrate the difference between the virtual method and the non-virtual method:
Using system;
Class A
{
Public void f () {console.writeLine ("a.f");}
Public Virtual Void g () {console.writeLine ("a.g");}
}
Class B: a
{
New public void f () {console.writeLine ("B.F");}
Public override void g () {console.writeLine ("b.g");}
}
Class test
{
Static void main () {
B b = new b ();
A a = b;
A.f ();
B.f ();
A.G ();
B.G ();
}
}
In this example, A is introduced into a non-virtual method F and a virtual method G. Class B introduces a new non-virtual method F to hide inheritance F, and also rewritten the method of inheritance G. This example produces output:
A.f
B.F
B.G
B.G
Note that statement A.G () actually calls B.G instead of A.G. This is because, the runtime type (ie, B) that is determined to call which actual method is implemented, not the compile time of the instance (ie, a).
Since the method declared in a class can hide the inheritance method, the same class can contain several virtual methods with the same signature. This will not cause polymity issues, because other methods are hidden in the method of derivating the degree of derivation. In the example below
Using system;
Class A
{
Public Virtual Void f () {console.writeLine ("a.f");}}
Class B: a
{
Public override void f () {console.writeLine ("B.F");}
}
Class C: B
{
New public virtual void f () {console.writeLine ("c.f");}
}
Class D: C
{
Public override void f () {console.writeLine ("D.f");}
}
Class test
{
Static void main () {
D = new d ();
A a = d;
B b = D;
C c = D;
A.f ();
B.f ();
C.f ();
D.f ();
}
}
Both Class C and D include two virtual methods with the same signature: a virtual method introduced in the introduced virtual method. However, the method introduced by C hides the method of inheritance from a. Therefore, the rewriting declaration of D is rewritten is the method of introduced by C, and D cannot rewrite the method introduced by A. This example produces output:
B.F
B.F
D.f
D.f
Note that by accessing the instance of D (with a type of derived degree, its method is not hidden), you can call the hidden virtual method.