C # rewriting and overloading of review

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.