What is inherited
Professional angle: B inherits class A, B is called a subclass of A, derived classes, A, called parent B, the base class, B class and superclass object class B uses all of the attributes and methods of class A.
Literally: inherit is to inherit all the assets of parents
class Person:
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex
class Dog:
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex
class Cat:
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex
Inherited advantage (use)
- Save code, reduce repetitive code
- Enhanced coupling (i.e. enhancement code readability)
- Make the code more standardized
- Subclass can call all the properties of the parent class
Three categories defined above, humans, dogs, cats, found that the above code has a lot of duplicate code, three classes have the same attributes, it is necessary to repeat the code together, redefining a class of animals, then you can delete code, this is changed to the following
Inherited usage
class Animal:
live = '有生命的'
def __init__(sefl, name, age, sex):
self.name = name
self.age = age
self.sex = sex
def eat(self):
print('动物都需要进食')
class Person(Animal): # 括号中Animal就是父类
pass
class Dog(Animal): # 括号中的Animal就是父类
pass
class Cat(Animal): # 括号中的Animal就是父类
pass
# 类名后面括号中传入的参数就是父类
# 1. 从类名执行父类的属性
先打印一下Person类中所有的属性
print(Person.__dict__) #输出结果:{'name': '小胖', 'age': 18, 'sex': '男'}
print(Persion.live) # 输出结果:有生命的
# 2. 类名可以执行父类的方法
Person.eat(555)
# 3. 从对象执行父类的一切
实例化对象一定会执行三件事.一定会执行__init__
p1 = Person('dsb', 21, 'boy')
print(p1.__dict__)
print(p1.live)
p1.eat()
print(f'p1 ---> {p1}')
# 注意: 子类以及子类对象只能调用父类的属性以及方法,不能操作(增删改).
Inherited classification
Single inheritance
Multiple Inheritance
Person Dog Cat: called subclass or derived class
Animal: is called the parent class, the base class, superclass
Single inheritance usage
class Aniaml(object):
live = '有生命的'
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex
def eat(self):
print(f'self ---> {self}')
print('吃东西')
class Person(Aniaml):
def eat(self):
print('人类需要进食')
class Cat(Aniaml):
pass
class Dog(Aniaml):
pass
p1 = Person('dsb', 21, 'boy')
p1.eat()
# 输出结果
人类需要进食
# 说明一个问题: 子类将父类的方法,覆盖了,(重写父类的方法) 查找位置
# 对象查找顺序,先从对象空间找名字,子类找名字,父类找名字
方法一: 第三个继承
人类的不同于其他狗类和猫类的特性
如何既要执行父类方法又要执行子类方法
class Aniaml(object):
live = '有生命的'
def __init__(self,name,sex,age):
self.name = name
self.age = age
self.sex = sex
def eat(self):
print(f'self ---> {self}')
print('吃东西')
class Person(Aniaml):
def eat(self):
print('人类需要进食')
def __init__(self,name, age, sex, hobby):
Aniaml.__init__(self, name, age, sex) # 关键点,手动的把对象空间传给self,但是()中需要接收参数
self.hobby = hobby
class Cat(Aniaml):
pass
class Dog(Aniaml):
pass
# 正常子类有__init__就不执行父类的__init__,现在的目的既要执行父类方法又要执行子类方法
p1 = Person('催牛逼') # 这里的传参方法:p1 = Person('对对哥', 23, '不详','吹牛逼')
print(p1.__dict__)
# 方法二
class Aniaml(object):
live = '有生命的'
def __init__(self, name, sex, age):
self.name = name
self.age = age
self.sex = sex
def eat(self):
print(f'self ---> {self}')
print('吃东西')
class Person(Aniaml):
def eat(self):
print('人类需要进食')
def __init__(self, name, age, sex, hobby):
super(Person, self).__init__(name,age,sex) # 关键点
# 简写成super().__init__(name, age, sex) # 使用了父类的init
self.hobby = hobby
class Cat(Aniaml):
pass
class Dog(Aniaml):
pass
# 正常子类有__init__就不执行父类的__init__,现在的目的既要执行父类方法又要执行子类方法
p1 = Person('对对哥', 23, '不详', '吹牛逼')
print(p1.__dict__)
Sentence summary
Single inheritance is designed to save the code, the definition of a common parent, but if the same exists in the parent class method subclass, the subclass method in the parent class will be overridden by the object name in the Find when the first heavy object space name lookup, if there is a direct quote, if not continue to find the name from the subclass space, if it is not to continue the search from the parent class name space
The use of multiple inheritance
class God:
def __init__(self, name):
self.name = name
def fly(self):
print('会飞')
def climb(self):
print('神仙累了也需要爬树')
class Monkey:
def __init__(self,sex):
self.sex = sex
def climb(self):
print('爬树')
class MonkeySun(God,Monkey): # 继承了2个类,孙猴子既是神仙又是猴子
pass
# 多继承的难点就是继承顺序的问题
# 现在执行的时候是先执行神仙的还是先执行猴子的呢?
这里面就涉及到单进程与多进程的分类:
面向对象:
python2.2之前都是经典类
python2.2之后到python2.7之间存在2种类型:经典类,新式类.区别:经典类是基类,不继承object,它的查询规则,依靠的是深度优先的原则,
python3x之后新式类,新式类必须继承object,查询规则是mro算法,在多进程的时候查询顺序不同
Multiple inheritance and single inheritance difference
Single inheritance only inheritance a parent class, multiple inheritance need to inherit more than one parent class, multiple inheritance this time a problem arises is that the order of succession issues, which in the end of the first inherited the parent class, which is the parent class method of execution
Multiple inheritance of difficulty: the order of succession issues
Classic depth search order
Principle: in the classic category are recorded using a depth-first traversal ⽅ case what is the depth-first way is ⼀ come first and then come back to find the record the next one....
The new class of multiple inheritance
mro algorithm
Universal Calculation
mro(Child(Base1,Base2)) = [ Child ] + merge( mro(Base1), mro(Base2), [ Base1, Base2] )
example
mro(Foo(H,G)) = [Foo] + merge(mro(H), mro(G),[H,G])
Header:
The first element of the list
Footer:
Collection of elements other than a list of tables head (epitopes may be empty)
Header, epitope
[A, B, C] A header [B, C] Footer
[A]: Header: A footer: []
Work on Four new calculation method of the class
example
'''
class O:
pass
class D(O):
pass
class E(O):
pass
class F(O):
pass
class B(D,E):
pass
class C(E,F):
pass
class A(B,C):
pass
# a = A()
# a.func()
'''
'''
mro(A) = mro(A(B,C))
= [A] + merge(mro(B), mro(C), [B,C])
mro(B) = mro(B(D,E))
= [B] + merge(mro(D), mro(E), [D,E])
= [B] + merge([D,O], [E,O], [D,E])
= [B,D] + merge([O], [E,O], [E])
= [B,D,E,O]
mro(C) = mro(C(E,F))
= [C] + merge(mro(E), mro(F),[E,F])
= [C] + merge([E,O],[F,O],[E,F])
= [C,E] + merge([O],[F,O],[F])
= [C,E,F,O]
mro(A) = mro(A(B,C))
= [A] + merge([B,D,E,O], [C,E,F,O], [B,C])
= [A,B] + merge([D,E,O], [C,E,F,O], [C])
= [A,B,D] + merge([E,O], [C,E,F,O], [C])
= [A,B,D,C] + merge([E,O], [E,F,O])
= [A,B,D,C,E] + merge([O], [F,O])
= [A,B,D,C,E,F,O]