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。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class CellPhone { func call() { print("Call someone!") } func powerUp () { print("Welcome!") } func powerDown() { print("Goodbye~") } func sendMessage() { print("sent a Message!") } } |
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。
besides,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
1 2 3 4 5 6 7 8 9 10 11 12 |
class CellPhone { var number = 0 var battery = 100 func call() { } func message() { } } |
We write subclasses to inherit the parent class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Phone:CellPhone { } class SmartPhone:CellPhone { func eMail() { } func internet() { } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class 武器 { var 模型 = "" var 耐久度 = 0.0 var 攻击力 = 0.0 var 等级需求 = 0 var 攻击类型 = 0 func 攻击() {} func 防御 () {} func 警戒() {} } |
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:
1 2 3 4 5 6 7 |
class 枪械:武器 { var 弹药 = 0 var 弹夹 = 0 override func 攻击() { } override func 防御() { } } |
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:
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 LogStudio:R0uter's Blog » Recalling once again:Inheritance
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1118.html
Comments