继承
今日内容大纲
- 初识继承
字面意思:儿子完全可以使用父类的所有内容
专业角度: 如果 B类继承 A类,B 类就称为子类,派生类
A类就称为父类,基类,超类
面向对象的三大特性之一:继承,封装,多态
2.继承的优点
1.减少代码的重复性
2.增加类之间的耦合性
3.代码更加清晰,流畅
3.单继承
1.类名执行父类属性方法
2.对象执行父类属性方法
3.在子类中既执行子类方法又执行父类方法
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
# class Cat(object):
# pass
cat1 = Cat()
# Human,Dog,Cat 子类,派生类.
# Animal 父类,基类,超类.
# person = Human('李业', '男', 18)
# print(person.name)
4.多继承
1.经典类:不继承 object 类,深度优先原则
2.新式类:继承 object 类.mro(c3)算法
class A(object):
# pass
# class A:
# pass
#
#
# class B(A):
# pass
#
# class C(B):
# pass
# class A:
# pass
#
#
# class B(object):
# pass
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()
# 经典类: 深度优先.从左至右,深度优先.
class O:
name = '太白'
class D(O):
pass
class E(O):
name = '李业'
# pass
class F(O):
name = '宝元'
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(D,E)) = [B] + merge(mro(D),mro(E),[D,E])
mro(B(D,E)) = [B] + merge([D,O],[E,O],[D,E])
mro(B(D,E)) = [B,D] + merge([O],[E,O],[E])
mro(B(D,E)) = [B,D,E] + merge([O],[O])
mro(B(D,E)) = [B,D,E,O]
mro(C) = [C,E,F,O]
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]
'''
# print(A.mro())
单继承课堂练习
class Base:
def __init__(self, num):
self.num = num
def func1(self):
print(self.num)
class Foo(Base):
pass
obj = Foo(123) # 实例化一个对象
obj.func1() # Foo 里找不到就去父类找
# 123
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)
print(obj.__dict__)
# obj.func1()
# {'num': 123}
# Foo. func1 123
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
class Base:
#
def __init__(self, num):
self.num = num
def func1(self): # self= obj
print(self.num) # 123
self.func2() # obj.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): # self = Foo(3)
# 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