C #(11. polymorphism)

Source: Internet
Author: User

11. Polymorphism

Object-oriented languages use virtual methods to express polymorphism. This means that the derived class can have the same signature method as the parent class, and the parent class can call the method of the derived class) ]. In Java, the method is virtual by default. In C #, you must use the virtual keyword to make the method called by the parent class.

In C #, you also need the override keyword to specify a method that will overload (or implement an abstract method) the method of its parent class.

Class BFile ://【: Class B]

{

Public virtual void foo (){}

}

Class D: BFile ://【: Should be class D: B]

{

Public override void foo (){}

}

An attempt to reload a non-virtual method will result in a compilation error, unless the "new" keyword is added to the method to indicate that the method is intended to hide the method of the parent class.

Class N: DFile ://【: Should be class N: D]

{

Public new void foo (){}

}

N n = new N ();

N. foo (); // call N's foo

(D) n). foo (); // call foo of D

(B) n). foo (); // call foo of D

Compared with C ++ and Java, the override keyword of C # makes it clear to read the source code which methods are overloaded. However, the use of virtual methods has advantages and disadvantages. The first advantage is to avoid using virtual methods to slightly increase the execution speed. The second point is to clearly know which methods will be overloaded.

Note: from now on, these statements are obviously not logical, but the original article is like this:

"However, requiring the use of the virtual method has its pros and cons. the first pro is that is the slightly increased execution speed from avoiding virtual methods. the second pro is to make clear what methods are intended to be overridden. ".

I think the logic will be smoother if the method I marked as italic is changed to keyword. In this way, the first sentence can be considered to be a comparison with Java, because its method defaults to virtual, and the second sentence is mainly a comparison with C ++ ]. However, advantage may also be a disadvantage. The final modifier is ignored by default in Java and Java.

You can use the final keyword in Java to lock the method, this is equivalent to the absence of virtual keyword modifier/member functions in C #/C ++.

This makes your program slightly less efficient. In C ++, it may impede scalability. Although this is true for the basic class, is unpredictable.

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.