Rewrite: it refers to the method for the subclass to redefine the parent class virtual function.
Public class Employee
{
Virtual public void CalculatePlay ()
{
Console. WriteLine ("Employee ");
}
}
Class SalariedEmployee: Employee
{
Public override void CalculatePlay ()
{
Console. WriteLine ("Salary ");
}
}
Static void Main (string [] args)
{
Employee em = new SalariedEmployee ();
Em. CalculatePlay ();
}
It means that the methods in the base class are identified by the virtual keyword, And Then override the class in the inheritance class. In this way, the methods in the base class have been overwritten and functions have been lost. When the reference of the base class object is directed to the object of the inherited class (polymorphism), this method is called to inherit the class method.
The rewrite method must have the same method signature, including the same method name, the same parameter list, and the same return value type.
The same method name, different parameter lists (different number of parameters or different parameter types), and different return value types exist in a class.
Is overloading, that is, reload
Method hiding: no matter whether the method in the base class uses the virtual keyword or not, the new Keyword can be used in the inherited class. (if the new keyword is not used, no errors will be generated, but a compilation warning will be generated) hiding Methods in the base class means that the original (in the base class) does not exist, while hiding is the original. Therefore, when the reference of a base class object is directed to an object that inherits the class (polymorphism), calling this method is the method of the base class called.
Do not declare the base class method (non-virtual by default). Use new in the derived class to declare the hiding of this method. When you access the parent class, the method of the parent class is called. When you access the Child class, the method of the Child class is called.
Public class Employee
{
Public void Fn ()
{
Console. WriteLine ("Employee ");
}
}
Class SalariedEmployee: Employee
{
Public new void Fn ()
{
Console. WriteLine ("SalariedEmployee ");
}
}
Static void Main (string [] args)
{
SalariedEmployee se = new SalariedEmployee ();
Se. Fn ();
Employee em = new SalariedEmployee ();
Em. Fn ();
}
}
Result:
SalariedEmployee
Employee