Polypeptide? Polymorphic!

We finished the last lesson inheritance,That this lesson we will continue in-depth,What to look at inheritance tree is big move。

You and I could talk about this polymorphic pronunciation,Your first thought should be a high school biology talked of "polypeptide";Ok,The only possibility is to pronounce the same between the two。

Inherited significance

We say that to achieve the extraordinary significance of inherited,It greatly reduces the number of redundant lines in our code,Reducing the difficulty of maintaining the code ......

In fact,I did not mention the inherited another sense - Contract。

A lesson we did a Demo,On behalf of the sub-class inherits the parent class to get all inheritable properties and methods - in other words,We can also interpret it this way:Ensure subclass inherits all inheritable have properties and methods of the parent class。

You see,We wrote a member having such as "weapons" category,Then all subclasses under weapons would have "weapons" class properties and methods - even if the method is overridden - it does not matter,However, this method still exists。

Back to the stem on the remote control and the TV

That is,Regardless of how the evolution of this TV,I always have a new generation of remote control that has the first few function buttons。

We declare a reservoir,Then put inside is not value,Instead of referring to - this is also the type of reference,It contains the properties and methods of the object referenced by the button ......。So we can not then it points to a completely different object can only point to the same first-class objects。

But we understand inheritance,It has generated a problem - the old button on the remote control seems to have come into effect on the next generation of television。

Yes! This is the multi-state!

We create a reservoir,The type is specified as a reference to a particular class,But if we are pointing to a subclass of this class - yes,You can compile! This is allowed!

Such,We can store a variety of state! --This is


So ...... Let's take a chestnut it!

We declare an array of weapons, although Weapon array types,But in fact they are all put to different subclasses of Weapon。

We iterate,Executive subclass of fire inside() Technique,The results obtained are also different。We say here is different subclasses of class status Weapon。 - So called polymorphism。

Now,We try to make use of multi-state object is passed around as parameters:

You see,We handle class pass in ...... just like these subclasses are applied with a "contract",We only need to follow this "contract" action,So we do not care passed in a subclass of exactly how to achieve this method,Anyway, they have to have this method on the right。


Yes,Declare a parent class reservoir can reference all of the corresponding sub-class!

This is the multi-state mechanism。Through this mechanism,We can reach a higher level of collaboration。As a program's source code,Someone has to realize the underlying superstructure was realized ...... now have polymorphic,Our initial design software architecture ordained inheritance tree,Then wait for the upper and lower latency and no time on,It can be carried out at the same time - they are no longer relevant,We do not need to upgrade their influence each other - because we have a "contract",The caller simply call the parent class like!

Original article written by Gerber drop-off:R0uter's Blog » Polypeptide? Polymorphic!

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1121.html

About the Author


The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。


Leave a Reply

Your email address will not be published. Required fields are marked *