The so-called overload refersSame classTwo or more of them have the same nameDifferent parameters.Overload must occur in a class., The function name is the same, the parameter type or order is different to constitute the overload, and has nothing to do with the return type.
Override:Overload is also weighed. Writing refers to the sub-class for the parent class.Virtual or Abstract Functions"Overwrite" (that is why some books translate overload into overwrite), but this "Overwrite" is different from the new keyword.
New:Override refers to two or more return types, method names, and parameters of different classes (base classes or derived classes) are the same, but the method bodies are different.
However, this coverage is a kind of surface coverage, which is also calledHideThe overwritten parent class method can be called.
Conditions for overwriting:
Overload must occur in a class. The function name is the same, and the parameter type or sequence is different to the overload, which is irrelevant to the return type.
Rewriting must occur in the base class and derived class. Its class functions are modified by virtual, and the derived class is modified by override.
Overwrite: Write a non-virtual function with the same name as the base class (the parameters are different) in the subclass to hide the function in the base class. After compilation, A New Keyword is prompted.
Overload example:
Public void fun ()
{
Console. writeline ("I am F ");
}
Public void fun (int I)
{
Console. writeline ("I am F, I = {0}", I );
}
Override rewrite features:
The method declared by override is called the override base method. The override base method must have the same signature as the override method.
The base method to be rewritten must be virtual, abstract, or override. You cannot override non-virtual or static methods.
The override method and the virtual method must have the same access level modifier, and the accessibility of the virtual method cannot be changed.
You cannot use the new, static, or virtual modifier to modify the override method.
The override attribute declaration must specify the access modifier, type, and name exactly the same as the inherited attribute, and the overwritten attribute must be virtual, abstract, or override.
Overwrite example:
When no overwrite is used, the derived class inherits the base class. The result is as follows:
Class
{
Public void fun ()
{
Console. writeline ("I am F ");
}
}
Class program:
{
Static void main (string [] ARGs)
{
Program P = new program ();
P. Fun ();
Console. Read ();
}
}
// The result is: I am f
What if we overwrite the original method?
Class
{
Public void fun ()
{
Console. writeline ("I am F ");
}
}
Class program:
{
Public new void fun ()
{
Int I = 1;
Console. writeline ("I am F, I = {0}", I );
}
Static void main (string [] ARGs)
{
Program P = new program ();
P. Fun ();
Console. Read ();
}
}
// The result is: I am F, I = 1
What is the difference between new overwrite and reload:
When the parameters of the subclass and parent classes are different
When the base function is not a virtual function, the base function is hidden. (Because the subclass and the base class are not in the same range, they are not overloaded)
When the base function is a virtual function, the base function is hidden. (Because the subclass and the base class are not in the same range, they are not overloaded. Because the parameters are different, they are not overwritten)
When the child class and parent class parameters are the same
When the base function is not a virtual function, the base function is hidden. (Because the subclass and the base class are not in the same range, they are not overloaded. Because the base class is not a virtual function, it is hidden or not overwritten)
When the base function is a virtual function, the base function will be overwritten. (Because the subclass and the base class are not in the same range, they are not overloaded)
So why not rewrite it? We can make an example and test it. This example has already been used in the virtual function. Here we will repeat the problem to illustrate it:
class A
{
public virtual void Fun()
{
Console.WriteLine("I am F");
}
}
class Program:A
{
public override void Fun()
{
int i = 1;
Console.WriteLine("I am F,i={0}", i);
}
static void Main(string[] args)
{
A p = new Program();
p.Fun();
Console.Read();
}
}
We know that in the above example, the derived class has an override method for the base class, so the result is: I am F, I = 1
If we replace override with new, the Overwrite will be equivalent to the above result, but what is the actual result?
The actual result is: I am f
From this we know that when the base function is a virtual function, the base function does not overwrite it, But overwrites the Same Name function of the base function.
C # basic (III)-heavy load and coverage