"Fatal square":Multiple inheritance and protocols

Last time we mentioned agreement,But we talk about it the way an application,This lesson we take a deeper look at this class and used up almost exactly what profound esoteric protocol。

From now on,We again review the shameful inheritance tree once:

Class inheritance tree

Class inheritance tree

Here we wrote a weapon ...... is used for attack and defense。Then,As a game,Model weapons can not just only in one place,Otherwise development costs will be too high - we want to drain as much as possible the value of the code。

Communicate with the designers of our future,Designers thought,We can give role-playing games added accessories bar - accessories do have a bar of it ...... but we can go into jewelry as part of the weapons - such as those of cold steel model,Some narrow enough,But also to increase the role of the various attributes - such as increased attack power sword,Sword to increase the speed?

Ok,Good choice,Then,Now these weapons need to add these features - and how to do it?

Fortunately, we have inherited:

it is good,let us see,In this hierarchy, how to make a fuss - we can think of now is to upgrade

  • To "weapon" is the direct parent of the relevant code to add accessories,So that all subclasses are OK! --but,A cannon jewelry? Also called "internal forces" jewelry?
  • Each weapon you want to add a method to make jewelry alone! --Ok,Polymorphic gone,You as jewelry team quit your dead。
  • The "jewelry" into an abstract method,Then as to be able to realize their own weapon accessories,This would accord with the polymorphism,Called "internal forces" of the jewelry will not appear,But hundreds of jewelry can be used as a weapon to achieve it again it seems to be a bit too much!
  • Then,Why do not we come to an abstract class,And let all subclasses to inherit it? Inherit two parent!

To sum up look,Seems to be the third option is the most effort,Efficiency is the highest,but,Here we encounter a serious bug:

If a class can be a subclass of two parent at the same time - that is, it can inherit more than one parent class,So who is going to guarantee both the parent class will certainly not related it? Once they have that relationship where ancestors,That this could become a ring!

We put this simplified topology,A class inherits from two parent classes,The two parent classes and at the same time is a subclass of the same class - Check it out,Here there is a diamond-shaped box,The final bottom of the subclass inherits the top-most parent class members twice!

We will not go any further discussed here,This is called "Fatal box",Trust me,You do not want to deal with this problem - so,The compiler will not allow such a thing。

In fact, any subclass can only inherit from a class。

This time that someone might want to ask:I have seen,Even declare a default project template are inherited in several classes,For example,:

Ok,In fact,NSApplicationDelegate is declared:

It is a protocol。

protocol

All right.,This time,We have to re-learn what protocol - in fact, the real purpose of its egg is to solve the problems we encountered above,The reason why the lesson uses protocol,In fact, nothing is a way Quxianjiuguo。

Anyway,Protocol is a completely abstract class - so would a fatal problem does not occur in the above - because the sub-class to inherit an agreement (here say "inheritance" has inappropriate,Should be called "follow") must implement all abstract members in the protocol - thus avoiding the compiler can not determine the version of the problem。and,Also retained the mechanism of polymorphism!

then,In order to experience the consistency of use,Swift's inheritance syntax is also agreement follows the syntax,And both can be written together, separated by commas,It looks like ordinary statement。

But you should pay attention,These type in comma-separated,Only one class can be,Other agreements have to be。

Such,We can make weapons subclass needs to make jewelry jewelry directly to follow protocol can!

Other than that

We would also like to say that,Since the interface allows for various follow - no defined single,And it is from the rules require completely abstract,So it can use across the inheritance tree:

For example, I have a "armor" of the inheritance tree,So Armor Some models can also be made into jewelry,The same can also follow the "jewelry" protocol!

 

 

Original article written by LogStudio:R0uter's Blog » "Fatal square":Multiple inheritance and protocols

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

About the Author

R0uter

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 *