Single inheritance
The package and method of packaging responsibility to the attribute of an abstract class
Inheritance can achieve reuse of code, written in the same code need not be repeated
1) Syntax
class class name (parent name):
pass
Subclass inherits from the parent class, you can directly use the parent class has been packaged good
It should be under the responsibility of the subclass, the subclass-specific packaging properties and methods
class Animal: #def __init__(self): def eat(self): print("吃") def drink(self): print("喝") def run(self): print("跑") def sleep(self): print("睡") class Dog(Animal): def bark(self): print("汪汪") wangcai = Dog() wangcai.eat() wangcai.run() wangcai.drink() wangcai.sleep() wangcai.bark()
2) Terminology
Subclass the parent class, inheritance
The derived class, the base class, the derived
Transitive 3) inheritance
Has a parent class and subclass of attributes and methods of the parent class the parent class's package
class Animal: #def __init__(self): 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("我会飞") xtq = xiaotianquan() xtq.fly() xtq.run() xtq.bark() xtq.drink() xtq.sleep() xtq.bark()
1.2 Method of rewriting
override
Rewrite both cases the parent class
1. A method of covering the parent
A method with the same name defined in a parent class in subclasses
After rewriting method, at run time, just call the method in the subclass rewrite, and will not call the parent package
class Animal: #def __init__(self): def eat(self): print("吃") def drink(self): print("喝") def run(self): print("跑") def sleep(self): print("睡") class Dog(Animal): def bark(self): print("汪汪") classCAT (Animal): DEF Cache (Self): Print ( " catch mice " ) class xiaotianquan (Dog): DEF Fly (Self): Print ( " I can fly " ) # overrides the parent class method DEF Bark (Self): Print ( " as God is called " ) XTQ = xiaotianquan () xtq.fly () xtq.run () xtq.bark ()
2 extend the method of the parent class
The method comprises a method implemented subclass has implementation of the parent class
super (). The method of the parent class
super is a special class () to create an object, the scene is most commonly used when rewriting the parent class method, call the method in the parent class package implemented
Method 1 subclass re parent class in
2 where it is needed to use super (). Parent class method to invoke the superclass method of execution
Other position 3 of the code, for the needs of the subclass, the subclass-specific write code to achieve
class Animal: #def __init__(self): def eat(self): print("吃") def drink(self): print("喝") def run(self): print("跑") def sleep(self): print("睡") class Dog(Animal): def bark(self): print("汪汪") classCAT (Animal): DEF Cache (Self): Print ( " catch mice " ) class xiaotianquan (Dog): DEF Fly (Self): Print ( " I can fly " ) # overrides the parent class method DEF Bark (Self): # 1 for subclass-specific needs, writing code Print ( " the same God called " ) # 2 using super () call was originally in the parent class package of super (). Bark () # 3 add code to the other sub-categories of Print ( " $% $$% " ) XTQ =xiaotianquan () xtq.fly () xtq.run () xtq.bark ()
python2.0 need to call the parent's method, the name of the parent class method (self), not recommended. If you use the subclass name calling methods, it will form a recursive call, an infinite loop
Dog.bark(self)
1.3 private parent class attributes and private methods
1 subclass can not own internal process, direct access to the parent class's private property or private methods
class A: DEF the __init__ (Self): Self. __num1 =. 1 self.num2 = 10 DEF __test (Self): Print ( " % D% D " % (Self. __num1 , self.num2)) class B (A): DEF Demo (Self): # private property can not access the parent class 1 # Print ( "% d"% self._num1) # 2 can not call the private methods of the parent class # Self .__ the Test () Pass # create a subclass object b = B () # can not use the private property or private methods of the parent class in external print(b.num2) #b.__test()
2 subclass can indirectly access to the private property through a public or private methods of the parent class
class A: DEF the __init__ (Self): Self. __num1 =. 1 self.num2 = 10 DEF __test (Self): Print ( " Private Method D% D% " .% (Self __num1 , self.num2)) DEF Test (Self ): Print ( " public methods of the parent class D% " . Self% __num1 ) . Self __test () class B (a): DEF Demo (Self): # . 1 can not access the private attributes of the parent class #Print ( "% D" self._num1%) # 2 can not call the parent class Private method # Self Test .__ () # . 3 public access to the parent class attributes Print ( " % D " % self.num2) # . 4 call the parent public methods self.test () # Create a subclass object B = B () b.demo () # can not use the parent class's private attributes or outside the private method # Print (b.num2) # B .__ Test ()
Multiple Inheritance
Subclasses plurality parent class, and having the properties and methods of the parent class
class subclass name (the name of a parent class, the name of the parent class 2 ..):
pass
class A: DEF Test (Self): Print ( " Test Method " ) class B: DEF Demo (Self): Print ( " Demo Method " ) class C (A, B): Pass # Create Object C = C () C .test () c.demo ()
If the property or method of the same name exists between parent class inheritance should be avoided
MRO method of search order (to know)
For class provides a built-in attribute __MRO__ can see the method of search order
Found in the current class method to find the next class, the last class, from left to right
class A: DEF Test (Self): Print ( " A Test Method " ) DEF Demo (Self): Print ( " A Demo Method " ) class B: DEF Test (Self): Print ( " B Test Method " ) DEF Demo (Self): Print ( " B Demo method " ) class C (A, B): Pass # Create Object C = C () c.test () c.demo () #The method of determining the calling sequence of objects Print (C. __mro__ )
A test 方法
A demo 方法
(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)
python object base class for all classes
The new class and legacy (classic) class
The new base class to class object
Old (classic) class does not object to the base class is not recommended
python 2.0 no parent class object is not used
python 3.0 default object as the base class
The new class and legacy (classic) class method will affect the search order
If no parent, the proposed unified inherited from object
class name of the class (object):
pass
Polymorphism