day23
初识继承
字面意思:儿子可以完全使用父亲的所有内容
专业角度:如果B类继承A类,
B类就称为子类、派生类
A类就称为父类、基类、超类
面向对象三大特性:继承、封装、多态
继承:单继承、多继承
# class Human: # 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 继承 class Animal: def __init__(self, name, sex, age): self.name = name self.age = age self.sex = sex class Human(Animal): pass class Dog(Animal): pass class Cat(Animal): pass # Human,Dog,Cat 子类、派生类 # Animal 父类、基类、超类 person = Human("李业", "男", 18) print(person.name)
继承的优点
- 减少了重复代码
- 增加了类的耦合性(耦合性不宜多,宜精)
- 使得代码更加规范化,合理化
单继承
类名执行父类属性方法
class Animal: live = "有生命的" def __init__(self, name, sex, age): self.name = name self.age = age self.sex = sex def eat(self): print("动物都需要进食") class Human(Animal): live = "有思想的活着" # 1、类名执行父类属性方法 print(Human.live) Human.eat(111)
对象执行父类属性方法
查询顺序单向不可逆: 子类使用父类的属性方法,父类不能使用子类的属性方法.
class Animal: live = "有生命的" def __init__(self, name, sex, age): self.name = name self.age = age self.sex = sex def eat(self): print(self) print("动物都需要进食") class Human(Animal): body = "有头有脸" # 2、子类对象执行父类的属性方法 obj = Human('汪洋', '男', 48) print(obj.live) obj.eat() print(obj) # a1 = Animal('汪洋', '男', 48) # print(a1.body) # 报错,父类不能获取子类属性
在子类中既执行子类方法又执行父类方法
方法一:不依赖继承的
# class Animal: # def __init__(self, name, sex, age): # self.name = name # self.age = age # self.sex = sex # # # class Human: # def __init__(self,name, sex, age, hobby): # ''' # # :param name: 李业 # :param sex: 男 # :param age: 18 # :param hobby: 旅游 # ''' # # self = obj # # Animal.__init__(人类对象,姓名,性别,年龄) # Animal.__init__(self,name,sex,age) # self.hobby = hobby # # # class Dog(Animal): # pass # # # class Cat(Animal): # pass # obj = Human('驴友') # obj2 = Human('抽烟') # print(obj.__dict__) # obj = Human('李业','男',18,'旅游') # print(obj.__dict__) # def func(self): # self = 666 # print(self) # # self = 3 # # func(self) # func(666) # def func1(a,b): # print(a,b) # # # def func2(argv1,argv2,x): # func1(argv1,argv2) # print(x) # # func2(1,2,666)
方法二: 依赖于继承
class Animal: def __init__(self, name, sex, age): self.name = name self.age = age self.sex = sex def eat(self): print('动物都需要吃饭') class Human(Animal): def __init__(self, name, sex, age, hobby): # Animal.__init__(self,name,sex,age) # super(Human,self).__init__(name, sex, age) #完整的写法 super().__init__(name,sex,age) # 执行父类的__init__方法,重构父类方法. self.hobby = hobby def eat(self): print(f'{self.name}都需要吃饭') # class Dog(Animal): # pass # # # class Cat(Animal): # pass # obj = Human('李业','男',18,'旅游') obj.eat() # print(obj.__dict__)
单继承练习题
# 1 class Base: def __init__(self, num): self.num = num def func1(self): print(self.num) class Foo(Base): pass obj = Foo(123) obj.func1() # 123 # 2 class Base: def __init__(self, num): self.num = num def func1(self): print(self.num) class Foo(Base): def func1(self): print("Foo. func1", self.num) obj = Foo(123) obj.func1() # Foo.func1 123 # 4 class Base: def __init__(self, num): self.num = num def func1(self): print(self.num) self.func2() def func2(self): print("Base.func2") class Foo(Base): def func2(self): print("Foo.func2") obj = Foo(123) obj.func1() # 123 # Foo.func2 # 再来 class Base: def __init__(self, num): self.num = num def func1(self): print(self.num) self.func2() def func2(self): print(111, self.num) class Foo(Base): def func2(self): print(222, self.num) lst = [Base(1), Base(2), Foo(3)] for obj in lst: obj.func2() # 111 1 # 111 2 # 222 3 # 再来 class Base: def __init__(self, num): self.num = num def func1(self): print(self.num) self.func2() def func2(self): print(111, self.num) class Foo(Base): def func2(self): print(222, self.num) lst = [Base(1), Base(2), Foo(3)] for obj in lst: obj.func1() # 1 # 111 1 # 2 # 111 2 # 3 # 222 3
多继承
引导 class ShenXian: def fly(self): print("神仙都会飞") def walk(self): print("神仙都会走路") class Monkey: def climb(self): print("猴子都会爬树") def walk(self): print("猴子会走路") class SunWuKong(ShenXian, Monkey): pass sun = SunWuKong() sun.fly() sun.climb() sun.walk()
python 类分为两种:
python2x:
python2.2之前,都是经典类
python2.2之后,经典类与新式类共存
python3x:
全部都是新式类
经典类
不继承object类,深度优先原则
深度优先原则:从左至右,深度优先
新式类
继承object类,mro(C3)算法
mro算法
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 obj = A() print(obj.name) # mro(Child(Base1,Base2)) = [ Child ] + merge( mro(Base1), mro(Base2), [ Base1, Base2] ) # mro(A(B,C)) = [A] + merge(mro(B),mro(C),[B,C]) """ 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]) mro(B) = mro(B(D,E)) = [B] + merge([D,O],[E,O],[D,E]) mro(B) = mro(B(D,E)) = [B,D] + merge([O],[E,O],[E]) mro(B) = mro(B(D,E)) = [B,D,E] + merge([O],[O]) mro(B) = mro(B(D,E)) = [B,D,E,O] mro(C) = mro(C(E,F)) = [C] + merge(mro(E),mro(F),[E,F]) mro(C) = mro(C(E,F)) = [C] + merge([E,O],[F,O],[E,F]) mro(C) = mro(C(E,F)) = [C,E] + merge([O],[F,O],[F]) mro(C) = mro(C(E,F)) = [C,E,F] + merge([O],[O]) mro(C) = mro(C(E,F)) = [C,E,F,O] mro(A(B,C)) = [A] + merge([B,D,E,O],[C,E,F,O],[B,C]) mro(A(B,C)) = [A,B] + merge([D,E,O],[C,E,F,O],[C]) mro(A(B,C)) = [A,B,D] + merge([E,O],[C,E,F,O],[C]) mro(A(B,C)) = [A,B,D,C] + merge([E,O],[E,F,O]) mro(A(B,C)) = [A,B,D,C,E] + merge([O],[F,O]) mro(A(B,C)) = [A,B,D,C,E,F] + merge([O],[O]) mro(A(B,C)) = [A,B,D,C,E,F,O] """ print(A.mro()) # [<class '__main__.A'>, <class '__main__.B'>, <class '__main__.D'>, <class '__main__.C'>, <class '__main__.E'>, <class '__main__.F'>, <class '__main__.O'>, <class 'object'>]
来源:https://www.cnblogs.com/NiceSnake/p/11379922.html