C # overwrite virtual Interfaces

Source: Internet
Author: User

Section 7 overwrite virtual Interfaces


Sometimes we need to express an abstract thing. It is a generalization of something, but we cannot really see it as an entity before our eyes, therefore, the object-oriented programming language has the concept of abstract classes. C # As an object-oriented language, the concept of abstract classes must also be introduced. Interfaces and abstract classes allow you to create a definition of component interaction. Through the interface, you can specify the methods required by the component, but do not actually specify how to implement the method. Abstract classes allow you to create behavior definitions and provide some common implementations for inheriting classes. Interfaces and abstract classes are useful tools for implementing polymorphism in components.


An abstract class must provide an implementation program for all the members of the interfaces listed in the basic class list of the class. However, an abstract class is allowed to map interface methods to abstract methods. For example



Interface IMethods {
Void F ();
Void G ();
}
Abstract class C: IMethods
{
Public abstract void F ();
Public abstract void G ();
}


Here, the IMethods implementation functions map F and G to the abstract method, and they must be overwritten in a non-abstract class derived from C.
Note that the explicit interface member implementation function cannot be abstract, but the explicit interface member implementation function can certainly call the abstract method. For example



Interface IMethods
{
Void F ();
Void G ();
}
Abstract class C: IMethods
{
Void IMethods. F () {FF ();}
Void IMethods. G () {GG ();}
Protected abstract void FF ();
Protected abstract void GG ();
}


Here, the non-abstract classes derived from C must overwrite FF and GG, so the actual implementation program of IMethods is provided.


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.