C # polymorphism

Source: Internet
Author: User

0. Polymorphism often manifests as "one interface, multiple functions".

1, polymorphism can be static or dynamic. In static polymorphism , the response of a function occurs at compile time . In dynamic polymorphism , the response of a function occurs at run time .

2. Static polymorphism

At compile time, the connection mechanism of functions and objects is called early binding, also known as static binding. C # provides two techniques for static polymorphism. The following were:

    • function overloading
    • Operator overloading

3, dynamic polymorphism

C # allows you to use keyword abstraction to create an abstract class that provides an implementation of a partial class of interfaces. When a derived class inherits from the abstract class, the implementation is complete. Abstract classes contain abstract methods, and abstract methods can be implemented by derived classes. Derived classes have more specialized functionality.

Note that here are some rules about abstract classes:

    • You cannot create an instance of an abstract class.
    • You cannot declare an abstract method outside an abstract class.
    • You can declare a class as a sealed class by placing the keyword sealedin front of the class definition. When a class is declared as sealed , it cannot be inherited. Abstract classes cannot be declared as sealed.

virtual methods can be used when there is a function defined in the class that needs to be implemented in the inheriting class. Virtual methods are declared using the keyword virtual . Virtual methods can have different implementations in different inheritance classes. A call to a virtual method occurs at run time.

Dynamic polymorphism is achieved through abstract classes and virtual methods .

Virtual and abstract

Both virtual and abstract are used to decorate the parent class by overriding the definition of the parent class to redefine the subclass.

    • The method of 1.virtual retouching must be implemented (even if only a pair of curly braces are added), and the method of the abstract modification will not be implemented.
    • 2.virtual can be overridden by a quilt class, and abstract must be overridden by a quilt class.
    • 3. If a class member is an abstract adornment, then the class must be added abstract, because only abstract classes can have abstract methods.
    • 4. An instance of the abstract class cannot be created and can only be instantiated by inheritance.

C # polymorphism

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.