C # overload (overload) method override (override) Hide (new)

Source: Internet
Author: User

I. Overload: a series of methods with the same name are defined in the same scope (such as a class), but the parameter list of methods is different. In this way, different parameters can be passed to determine which one to call.

It is worth noting that method overloading only determines which method to call based on different parameters, rather than the return value. As for the reason, it can be understood that if you call a method with multiple methods with the same name, the system must know which one you want to call. parameters can help the system obtain the answer at the method entrance, he knows which method to use based on the parameters you give. If only the return values are different, in many cases, the system cannot obtain valid judgment conditions, such as: Double method (); int method (); if you call: method () like this (). The system certainly does not know which one to call.

Example:

// The return value is the same and the parameters are different

Class billpayment {void paybill (INT telephonenumber) {// This method is used to pay the fixed phone fee} void paybill (long customernumber) {// This method is used to pay the electricity fee} void paybill (long customernumber, double amount) {// method used to pay Mobile Phone Fee }}}

 

// The return value is the same, and the number of parameters is different

class Add{    int addtion(int num1,int num2)    {        return num1+num2;    }    int addtion(int num1,int num2,int num3)    {        returnnum1+num2+num3;    }}

 

// Different parameters return different values

int Method(int[] numbers){}double Method(double[] numbers){}

 

Note: When a wildcard is displayed, methods with the same parameters and returned values can also constitute heavy loads, which are not described here.

 

2. Override: When Inheritance occurs, the methods in the parent class are redefined in the subclass. The methods in the subclass are the same as those in the parent class, that is, the method names, parameters, and return values are the same.

For example, if the base class method is declared as virtual (virtual), override is used in the derived class to declare the override of this method.

Override is generally used to rewrite methods of interface implementation and inheritance classes. Note the following:

1. The flag of the overwriting method must match the name and parameter of the overwritten method to achieve the overwriting effect;

2. The returned value of the overwritten method must be the same as that of the overwritten method;

3. The exception thrown by the overwriting method must be the same as that thrown by the overwriting method, or its subclass;

4. The method to be overwritten cannot be private. Otherwise, a new method is defined in its subclass and not overwritten.

Example:

Override the namespace method {class program {static voidmain (string [] ARGs) {baseclass CO = new classoverride (); Co. setname ("Override");} // The base class public classbaseclass {public virtualvoid setname (string name) {console. writeline ("base class: My name is" + name) ;}// derived class public classclassoverride: baseclass {publicoverride void setname (string name) {console. writeline ("override: My name is" + name );}}}}

In method rewriting, we introduced virtual methods. What is the difference between virtual methods and abstract methods?

 

1. There is an implementation part of the virtual method that can be inherited by the quilt class, so that the subclass gets the same method as the base class, and also provides the option to override this method for the derived class. On the contrary, an abstract method does not provide an implementation part. It is a method that forces a derived class to overwrite (otherwise, the derived class cannot be a specific class)

 

2. (abstract) abstract methods can only be declared in abstract classes. (virtual) virtual methods are not.

 

3. Abstract abstract methods must be rewritten in the derived class, while virtual methods do not.

 

4. (abstract) Abstract METHODS cannot declare method entities, but virtual methods can.

 

 

I personally feel that method overloading and method rewriting are a bit like polymorphism. There are multiple forms of the same method: Execute the "same" method, but they are implemented through their own implementation code, that is, in the same method, different objects will produce different results.

 

3. Hide (method): do not declare the base class method (non-virtual method by default). Use new in the derived class to declare the hiding of this method. When hidden, the method of the parent class is called when accessing the parent class, and the method of the subclass is called when accessing the Child class.

Example:

Namespace hiding {class program {static voidmain (string [] ARGs) {classnew Cn = new classnew (); CN. setname ("new"); baseclass BC = cn; BC. setname ("base class");} // base class public classbaseclass {public voidsetname (string name) {console. writeline ("base class: My name is" + name) ;}// derived class public classclassnew: baseclass {// if new is not used here, a warning is generated! New publicvoid setname (string name) {console. writeline ("New: My name is" + name );}}}}

Running result: New: My name is new.

Base Class: My name is a base class.

 

If the above is hard to understand, let's take another example of hiding static variables.

 

Namespace hidden inherited member {class program {public classbaseclass {public static int A = 123;} public classclassnew: baseclass {new public static int A = 456; static voidmain (string [] ARGs) {console. writeline ();}}}}

Result: 456

 

 

Note: not only are there methods to hide, but there are others. inheritance to hide names generally take one of the following forms:

 

1. Introduce constants, designation, attributes, or types in the class or structure to hide all base class members with the same name.

 

2. introduce methods in a class or structure to hide attributes, fields, and types with the same name in the base class. It also hides all base class methods with the same signature.

 

3. The introduced class or structure indexer will hide all base class indexers with the same name.

 

4. It is incorrect to use both new and override on the same member.

 

In addition, warnings are generated using the new modifier in the declaration that does not hide inherited members. Warnings are generated if the new modifier is not used in the hidden inherited member's life.

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.