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