Conditions for overloading: 1. Must be in the same Class 2. The method name must be the same 3. The parameter list cannot be the same.
Overridden Condition:
1. In different classes
2. Two methods that occur method override return value, method name, parameter list must be exactly the same
3. Subclass throws an exception that cannot exceed the exception thrown by the corresponding method of the parent class
4. The access level of the subclass method cannot be lower than the access level of the corresponding method of the parent class (public,package,protected, Private)
5. Method body Different
Super () call the parent class construct, super. Only instance methods of the parent class can be called
This () calls the other constructor methods of the class, this. Call the properties of the class, method
If the parent class has a parameter construct, the subclass must define the constructor method
Overload:
Overloads occur in the same class, and overloads are mainly for cases where the same method name is the same in the same class, but the arguments (must) differ or the return type (not required). You can refer to the following code
Class Overloadtest
{
public void Hello ()
{
}
public void Hello (String str)
{
}
public string Hello ()
{
}
}
In this code there are three methods with the same name Hello, they are overloaded with each other, note two points: they are common in a class, although the same name but do different operations; The first method and the third method have only the return type, which is not allowed in C #, and the compiler will quote " An error has been defined for a method named "Hello".
Rewrite:
Overrides occur between classes that have an inheritance relationship
Overrides occur on derived classes by overriding the Override keyword to override methods that inherit from their parent class with the same return value, method name, and parameter list.
Class Program
{
static void Main (string[] args)
{
OVERWRITETESTTT owtt = new Overwritetesttt ();
Owtt.hello ();
OVERWRITETESTTTT owttt = new Overwritetestttt ();
Owttt.hello ();
}
}
Class Overwritetest
{
Over write the method ToString ();
public override string ToString ()
{
Return "Hello World";
}
Define a virtual method Hello () to is over written
public virtual void Hello ()
{
Console.WriteLine ("Hello");
}
}
Class Overwritetesttt:overwritetest
{
public override void Hello ()
{
Console.WriteLine ("No Hello World");
}
}
Class OVERWRITETESTTTT:OVERWRITETESTTT
{
Over write the method Hello () of class OVERWRITETESTTT
public override void Hello ()
{
Base. Hello ();
Console.WriteLine ("NNo Hello World");
}
}
C # overloading and rewriting and using