Interface, virtual method, abstract method

An abstract method is a function that has only a definition and no actual method body. It can only appear in abstract functions and must be rewritten in subclasses; virtual methods have their own function bodies, which have provided function implementations, but are allowed in subclasses. Override or override in the class.
The rewritten subclass virtual function is covered.

Abstract method

Use the abstract keyword public abstract bool Withdraw(...);

Abstract methods are methods that must be overridden by derived classes.

An abstract method can be regarded as a virtual method without a realization body

If the class contains abstract methods, then the class must be defined as an abstract class, regardless of whether it also contains other general methods

Virtual method

Use the virtual keyword public virtual bool Withdraw(...);

When calling a virtual method, the runtime will determine what kind of instance of the calling object is and call the appropriate overridden method.

A virtual method can have an implementation body. If the declaration of an instance method contains a virtual modifier, the method is called a virtual method;

The implementation of a virtual method can be replaced by a derived class. The process of replacing the implementation of the inherited virtual method is called rewriting the method; in a virtual method call, the runtime type of the instance involved in the call determines which implementation of the method is to be called.

Limitations of virtual functions:

1.Virtual functions are only applicable to class objects with inheritance relationships, so only member functions of the class can be described as virtual functions.

2. A static member function cannot be a virtual function.

3. Inline functions cannot be virtual functions.

4 The constructor cannot be a virtual function.

5. The destructor can be a virtual function. To put it simply, abstract methods need to be implemented by subclasses. Virtual methods have been implemented, and subclasses can or may not be overridden depending on the requirements.

To put it simply, abstract methods need to be implemented by subclasses. Virtual methods have been implemented, and subclasses can or may not be overridden depending on the requirements. 

Both virtual methods and abstract methods can be overridden by derived classes. What is the difference between them?

1. A virtual method must have an implementation part, and an abstract method does not provide an implementation part. An abstract method is a method that forces a derived class to cover, otherwise the derived class will not be instantiated.

2. Abstract methods can only be declared in abstract classes, virtual methods are not. In fact, if the class contains abstract methods, then the class is also abstract and must be declared as abstract.

3. Abstract methods must be rewritten in derived classes, which is similar to interfaces, and virtual methods are not necessary.

Must be established under the inheritance relationship before they can be used

Distinguishing points: Virtual method: The virtual method is used when it is clear that the subclasses under the parent class have common methods and have their own characteristic methods.

Abstract method: The abstract method is to make it clear that the subclasses under the parent class have no common methods, and it is not clear how to write, and the subclasses have their own characteristics and methods to use

Interface: An interface is a characteristic method under an abstract method, which can be added as an abstract method under an abstract subclass.

Guess you like

Origin blog.csdn.net/weixin_37081112/article/details/103999047