A little knowledge about polymorphism

Polymorphism is one of the three basic characteristics of object-oriented, copy the definition first:

Polymorphism is the ability of the same behavior to have different manifestations or forms. Polymorphism is the use of different instances of the same interface to perform different operations. Polymorphism is the embodiment of multiple manifestations of an object.

As far as I understand it, similar to the mobile phone charging cable, the plug is separated from the data cable. If there is a plug, it does not necessarily have to be your own original data cable (like the new USB Type-C in addition to the USB Type-A and B interfaces ). Similar, you need to use the corresponding interface for different mobile phones, but use the same charging head. Improve scalability, replaceability, making it more convenient and flexible.

The necessary prerequisites for polymorphism are inheritance, overriding and parent class references to child class objects. That is to say, the steps of polymorphism must be: write the parent class, write the subclass, override the method, and the parent class refers to the child class object.

This is an automatic up-type conversion.

The manifestation of polymorphism: 1. Formal parameters of the parent class method 2. The return value type of the parent class method

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325162823&siteId=291194637