Second, Java object-oriented (9) _ object-oriented - polymorphic idea

2018-05-02

 

polymorphic thinking

 

Polymorphism is the ability to have multiple different manifestations or forms of the same behavior.

Polymorphism is the same interface, using different instances to perform different operations, as shown in the figure:

 

 

Polymorphism is the embodiment of multiple manifestations of an object.

In reality, for example, we press the F1 key for this action:

  • If the current pop-up in the Flash interface is the help document of AS 3;
  • If the current pop-up under Word is Word Help;
  • What pops up under Windows is Windows Help and Support.

The same event occurs on different objects will produce different results.

--------------------------------------------------------------------------------------------------

 

 For example, you are a Dionysian and have a soft spot for alcohol. When I came home one day, I found that there were several glasses on the table filled with white wine. From the outside, it was impossible for us to know what kind of wine it was. Only after drinking it could we guess what kind of wine it was. Once you drink it, it is Jiannanchun, drink it again, it is Wuliangye, and drink it again, it is Jiuguijiu... Here we can describe it as follows:

      Wine a = Jiannanchun//At this time, a represents the form of Jiannanchun

      Wine b = Wuliangye//At this time, b represents the shape of Jiannanchun type

      Wine c = Jiuguijiu//At this time, c represents the form of Jiannanchun type

      …

      What's here is polymorphism. Jiannanchun, Wuliangye, Jiuguijiu are all subclasses of wine. We can refer to different subclasses only through the parent class of wine . This is polymorphism - we only know when the reference variable is running. instance object .

 

Objects (a/b/c) have two types:

  compile type: the type of the declared variable, wine

  Run Type: True Type of Object, Jiannanchun/Wuliangye/Jiuguijiu

The compiled type must be the parent class of the runtime type (upcast) or the same as the runtime type (downcast). Polymorphism is when the compiled type is the parent of the runtime type.

 

In the drinking example above, wine (Win) is the parent class, and Jiannanchun (JNC), Wuliangye (WLY), and Jiuguijiu (JGJ) are the subclasses. We define the following code:

      JNC a = new JNC();

      It is very easy for us to understand that this code is nothing more than instantiating an object of Jiannanchun! But so what?

      Wine a = new JNC();

      Here we understand this, here defines a Wine type a, which points to the JNC object instance. Since JNC inherits from Wine, JNC can be automatically converted to Wine, so a can point to the JNC instance object.

   This has a very big advantage. In inheritance, we know that the subclass is an extension of the parent class, which can provide more powerful functions than the parent class. If we define a parent class reference type that points to the subclass, then it is in addition to In addition to being able to refer to the commonality of the parent class, you can also use the powerful functions of the subclass.

----------------------------------------------------------------------------------------------------------------------------------------------------

 

The realization conditions of polymorphism:

1. Inheritance (class and class) or interface (interface and implementation class)

2. Override (rewrite)

3. The parent class reference points to the subclass object (upcast)

 

Principle: When a super (parent) class object reference variable refers to a subclass object, the type of the referenced object rather than the type of the reference variable determines whose member method is called (that is, whose method is called by the type of the subclass object ),

   But the called method must be defined in the superclass, that is, the method overridden by the subclass.

----------------------------------------------------------------------------------------------------------------------------------

 

多态的特点:

  父类引用指向子类对象,在运行时期会表现具体的子类特征。

  指向子类的父类引用由于向上转型了,它只能访问父类中拥有的方法和属性,而对于子类中存在而父类中不存在的方法,该引用是不能使用的,尽管是重载该方法。

  如果子类重写了父类中的某些方法,在调用这些方法的时候,必定是使用子类中定义的这些方法(动态连接、动态调用)。

 

Guess you like

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