Three characteristics of object-oriented
- Package according to mandate the attributes and methods packaged into an abstract class in
- Inheritance to reuse implementation code , the same code need not be repeated written
- Polymorphic different object calls the same method, generating different results of increasing the flexibility of the code
01. single inheritance
1.1 The concept of inheritance, syntax and features
The concept of inheritance : a subclass has a parent class all the methods and properties
class Animal: def eat(self): print("吃") def drink(self): print("喝") def run(self): print("跑") def sleep(self): print("睡") class Dog: def eat(self): print("吃") def drink(self): print(" Drink " ) DEF RUN (Self): Print ( " Run " ) DEF SLEEP (Self): Print ( " sleep " ) DEF Bark (Self): Print ( " bark " ) # create an object - a dog objects wangcai = Dog () wangcai.eat () wangcai.drink () wangcai.run () wangcai.sleep () wangcai.bark ()
1) inheritance syntax
class class name (parent name): Pass
class Animal: DEF EAT (Self): Print ( " eat --- " ) DEF . Drink. (Self): Print ( " drink --- " ) DEF RUN (Self): Print ( " run --- " ) DEF SLEEP (Self): Print ( " sleep --- " ) class Dog (Animal): # child class has all the properties and methods of the parent class # DEF eAT (Self): # Print ( "eat") # # DEF. Drink. (Self ): # print ( "drink") # # DEF RUN (Self): # print ( "Run") # # DEF SLEEP (Self): # print ( "sleep") DEF Bark (Self): Print ( " bark " ) # create an object - the object dog wangcai = dog () wangcai.eat () wangcai.drink () wangcai.run () wangcai.sleep () wangcai.bark ()
- Subclass inherits from a parent class , you can directly enjoy the parent class method has been packaged, the need to develop again
- Subclasses should According functions , package subclass-specific properties and methods
2) Terminology
Dog
Class is aAnimal
class subclass ,Animal
class is theDog
class of the parent class ,Dog
class fromAnimal
class inheritsDog
Class is theAnimal
class of the derived class ,Animal
the class is aDog
class of the base class ,Dog
class fromAnimal
class derived
Transitive 3) inheritance
C
Class fromB
a class withB
class and fromA
class inheritance- Then the
C
class will have aB
class andA
all the properties and methods of the class
Subclass has the parent and the parent's parent class of all the packages in the properties and methods
class Animal: def eat(self): print("吃---") def drink(self): print("喝---") def run(self): print("跑---") def sleep(self): print("睡---") class Dog(Animal): def bark(self): print("汪汪叫") class XiaoTianQuan(Dog): defFly (Self): Print ( " I can fly " ) class Cat (Animal): DEF the catch (Self): Print ( " catch mice " ) # create a deified dog object XTQ = XiaoTianQuan () xtq.fly () xtq.bark () xtq.eat () xtq.catch ()
Question: deified dog can call the Cat
class defined in the catch
way?
Answer: can not , because the deified dog and Cat
not between the inheritance relationship.
1.2 Method of rewriting
- Subclass has a parent class all the methods and properties
- Subclass inherits from a parent class , you can directly enjoy the parent class method has been packaged, the need to develop again
Scenarios
- When the parent class when the implementation of the method can not meet the needs of the subclass, a method may be rewritten (the override)
Override the parent class method there are two cases:
- Covering the parent class method
- Methods of the parent class extend
1) covering the parent class method
- If in development, the parent class implementation and subclass implementation , completely different
- It can be used to cover a manner, in the subclass to rewrite the parent class implementation
Specific implementation, the equivalent subclasses defines a and the same name as the parent class and implements
After rewriting, at run time, just call the method in the subclass rewritten, and will no longer call the parent class package
class Animal: def eat(self): print("吃---") def drink(self): print("喝---") def run(self): print("跑---") def sleep(self): print("睡---") class Dog(Animal): def bark(self): print("汪汪叫") class XiaoTianQuan(Dog): def fly(self): print("我会飞") def bark(self): print("叫得跟神一样...") xtq = XiaoTianQuan() # 如果子类中,重写了父类的方法 # 在使用子类对象调用方法时,会调用子类中重写的方法 xtq.bark()