C # reload, rewrite, and use,

Source: Internet
Author: User

C # reload, rewrite, and use,
Reload condition: 1. It must be in the same class. 2. The method name must be the same. 3. The parameter list cannot be the same.
Rewrite condition:
1. In different classes
2. The return values of the two methods with method rewriting. The method name and parameter list must be completely consistent.
3. The exception thrown by the subclass 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. Different Methods

Super () calls the parent class construction. super. can only call the instance method of the parent class
This () calls other constructor methods of the class, this. Calls the attributes and methods of the class.
If the parent class has parameter constructor, The subclass must define the constructor.

Overload:

The overload occurs in the same class. The overload mainly occurs when the Chinese name of the same class is the same but the parameters (required) are different or the return type (not required) is different. 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 Hello with the same name. They are overloaded with each other. Note: they exist in a class, although the same name is used, different operations are executed. The first method and the third method only have different return types, which is not allowed in C, the compiler reports the error "A method named" Hello "has been defined.


Rewrite:

Override occurs between classes with inheritance relationships
Override occurs on a derived class. The override keyword is used to override methods inherited from its parent class that have 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 be 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 ");
}
}
 

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.