Last time we specifically about the inheritance mechanism,And also designed an inheritance tree,So the question is:I'm not going to ask Excavator strong Which? I want to ask how to determine whether a class is a subclass of another class do? How do we design a class instead of a class of attributes?
"A" and "a"
Here we will use such a method to examine the relationship between the two of them:
we say,Pistol firearms --OK,no problem,So pistol This class is a subclass of firearms。
There is a sword sword,It is a cold steel - no problem,Such a sequence that came out of the inheritance tree。
Or ...... phone is a monitor? --Ok,Phone has a display might be more appropriate,So is the phone's display attributes of a。
Monitor diode --OK,Diode display attribute is。
We rely on such a way of thinking,Quick screening inheritance tree design is logical。
correct
This method is not only about testing and also between the two,An inheritance tree the bottom of the class should be able to have the class "is a" very top of this relationship - such as "pistol is the weapon" so,if not,What about your inheritance tree design, I am afraid there will be a problem。
Order issue
We have been saying inheritance tree,So it was a tree topology,Subclass must have a parent class,However, the parent may not necessarily have the subclass - so that the parent does not know for subclasses,in contrast,In subclass, we can use the super keyword to call the parent class's method,For example, I want to expand it attacks firearms,We can do it this way:
1 2 3 4 5 6 7 |
class RPG:枪械 { override func 攻击() { //攻击前的特殊准备 super.攻击() //攻击后的特殊动作 } } |
My diary it?
Remember we said the diary and private metaphor it? Marked as private methods and properties can not be read by someone else! In the case of your child without your permission same can not peek at your diary!
So we say that the subclass inherits all the members of the parent class time,其实是不包括私有方法和属性的!这些成员只在本类里可见哟~
Original article written by LogStudio:R0uter's Blog » In the end how to do:"A" and "a"
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1120.html
Comments