Recalling once again:Inheritance

This time we take a look at a few lessons in succession before mentioned,We had初见 OPEN In a development with a mobile phone (system) to describe inherited this thing chestnuts,I believe we have the impression。

Inheritance

So this lesson we will learn to understand the depth of the concept of inheritance。

This is actually not difficult to understand,You see,When your parents died,Then they will be up to you to inherit wealth - Well,This is not a very happy thing worth,But this is the fact that。

And also,For example, a whole family's father retired,His company run by his son,We said it was "inherited his father"。

Finally, I want to return inside Swift,Is a member of a subclass inherits the parent class。Here we talk about "members",It refers to the properties and methods inside the class。

For chestnut bar:This is a class of mobile phones

We write subclasses to inherit the parent class:

For the parent who CellPhone,It is more abstract,How abstract law? Any one can be regarded as a mobile phone device,Definitely has a function - dial-up,Phone,Texting。

For subclasses we do production of mobile phone,It is only the most basic functions,So phone only needs to inherit from class like CellPhone,Although it did not write any of the properties and methods,But it was born with the CellPhone all the properties and methods。

For our smartphone smartPhone ,We have added email and Internet access features,But did not achieve the basic functions of a mobile phone - but it inherited from CellPhone,All functions so it naturally also with the phone。

As for the power and number two properties,Two subclasses can set their own content。

Design of an inheritance tree

Let's say we want to develop games,This game also has the technology of both martial arts,Both heroes have beauty - so that there is definitely a war - there is beauty Well。With war,There should be weapons,Then we can create a weapon on inheritance tree。

First of all,Designers tell us,Total have several weapons:machine gun、pistol、rifle、Sword、Machete、Stick、Internal forces (this can be considered?)、Hammer、Crossbow arrow、Bow and arrow、Well hidden weapon ......,Enough。

first step,Identify objects with common attributes and behavior

Ask yourself:What these weapons have in common?

Ah ...... this is too much trouble,Why not just a class for each weapon to do?

- It is not impossible,But then it will certainly have a lot of duplicate code,This will greatly increase the total number of lines of code,It is not conducive to maintaining a,Once we want to modify some of the properties of these weapons it? One by one to change it! another,Is your bug number of programs and is proportional to the number of lines of code ...... so we need to have the responsibility and obligation to ensure that the code is simple。

All right,These weapons will certainly have to have a property to hold their model,There have durability, right? There was attack,Range attack,But also a type of attack,There are so many temporary grade requirements ...... bar

Their behavior,For example attack,defense,Such vigilance ......,We have three methods:

Such,We will be able to inherit from a wide variety of weapons and weapons here! We calculate to,Suppose the designer to 100 specific weapons,Do we really want to top this code is repeated a hundred times to write it? ! (Pupils have bothered so friends)

All right,Now let us look at,Weapon cold and hot weapons are different,Their attack is completely different,Knives and swords can Slashing,But we must bow and crossbow shooting,We let themselves subclasses override the parent class method - say back cover,Simple usage you know。

But there is a problem,Machine gun pistol bullets must have clip,These seems can be extracted - yes,Among the many subclasses inside,You can find more abstract method is a good choice,We have to design another weapon subclass but is the parent class of firearms:

Such,We use firearms to inherit this class a subclass of words,Then it had ammunition and cartridges,There are firearms belonging to attacking and defending methods。

Complete class inheritance tree

In accordance with the top design ideas,We will now complete the inheritance tree:

Class inheritance tree

Class inheritance tree

and many more,Which method call in the end?

Now we have inheritance tree,Also inherited in an inheritance tree ...... but where the final instance method to determine how this time call it in?

First of all,Now that we have inheritance tree,Then the compiler must also be able to know,That is sure to find a way to call you。

Secondly,Generally we follow a recursive way to query,The inheritance tree from the bottom start (that is, the most specific object starts),Find up layer by layer,Know where to find a corresponding - a method that is closest to the object of that method is that we want to call。

For example, we create an instance of a pistol,Pistol and did not attack methods,We call Attack,The compiler will find online,Find a parent class overrides the method of attack - not to find the top level of the most primitive method of attack。

Original article written by Gerber drop-off:R0uter's Blog » Recalling once again:Inheritance

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1118.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。

Comments

Leave a Reply

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