Overload: In the same scope, the same parameter method with the same name, but the return value cannot constitute a overload.
Override: During the inheritance process, the Child class overrides the parent class method, and the Parent and Child class methods are the same.The base class method is declared as virtual (virtual), and used in the derived classOverrideDeclare that this method is overwritten.
Hide: do not declare the base class method (non-virtual method by default ).NewHide this method.
During overload, select the method to be called Based on the parameter;
When rewriting, accessing the parent subclass calls the override method of the subclass;
When hidden, accessing the parent class calls the method of the parent class and the method of the subclass class.
Hidden example:
1 Using System;
2 Class A
3 {
4 Public Void F ()
5 {
6 Console. writeline ( " A.f " );
7 }
8 }
9 Class B:
10 {
11 New Public Void F ()
12 {
13 Console. writeline ( " B. f " );
14 }
15 }
16 Class Test
17 {
18 Static Void Main ( String [] ARGs)
19 {
20 B = New B ();
21 B. F ();
22 A = B;
23 A. F ();
24 }
25 }
The output is
B. f
A.f
Virtual method rewriting example
Using System;
Class A
{
Public Virtual Void F ()
{
Console. writeline ( " A.f " );
}
}
Class B:
{
Public Override Void F ()
{
Console. writeline ( " B. f " );
}
}
Class Test
{
Static Void Main ()
{
B = New B ();
B. F ();
A = B;
A. F ();
}
}
The output is B. f
B. f
Supplement: override is generally used for method rewriting of interface implementation and inheritance classes. Note that
1. the logo of the covered method must match the logo of the covered method to achieve the coverage effect;
2. The returned value of the overwritten method must be the same as that of the overwritten method;
3. The exception thrown by the overwriting method must be the same as that thrown by the overwriting method, or its subclass;
4. The method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.