01 昨日内容回顾
类的名称空间 对象的空间
class A:
def __init__():
pass
obj = A()
对象查询属性:对象本身 ----> 类 -----> 父类
类查询属性:类本身 -----> 父类
组合:一个类的属性是另一个类的对象。
class A:
def __init__(self,name):
self.name = name
def func(self,obj):
self.aaa = obj
class B:
pass
b = B()
o = A('barry')
o.func(666)
o.func(b)
python三大特性之一:继承,封装,多态。
继承:
单继承,多继承。
单继承:
class A:
def __init__(self,name):
self.name = name
def func(self,obj):
self.aaa = obj
class B(A):
def func(self,obj,obj2):
# A.func(self,obj)
#super(A,self).func(obj)
super().func(obj)
print(obj2)
b = B('wusir')
#b.func(111)
b.func()
# 既要执行子类func 又要执行父类的func
b.func(666,777)
02 作业讲解
03 类与类之间的关系
依赖关系
关联关系
聚合关系
组合关系
......
04 多继承
# 03 类与类的关系
# 依赖关系# 大象走进冰箱,并关闭# 基本类# class Elphant:# def __init__(self,name):# self.name = name# def open(self):# """# 开门# :return:# """# pass# def close(self):# """# 关门# :return:# """# pass## class Refrigerator:# def __init__(self,name):# self.name = name## def open_door(self):# print('冰箱门被打开啦')## def close_door(self):# print('冰箱门被关闭啦') # 接下来# class Elphant:# def __init__(self, name):# self.name = name## def open(self,obj):# """# 开门# :return:# """# print('心里默念:1,2,3,开!')# obj.open_door()## def close(self):# """# 关门# :return:# """# print('心里默念:3,2,1关!')### class Refrigerator:# def __init__(self, name):# self.name = name## def open_door(self):# print('冰箱门被打开啦')## def close_door(self):# print('冰箱门被关闭啦')## e1 = Elphant('神奇的大象')# media = Refrigerator('美的')# e1.open(media)# 依赖关系:给一个类的方法传了一个参数此参数是另一个类的对象(类名)。# 这种依赖关系是所有关系中紧密型最低的,耦合性最低的。# 冰箱依赖大象,就好比企业与兼职工的关系,你中有我,我中没有你。 # 关联关系 聚合关系 组合关系 # class Boy:# def __init__(self, name, girlfriend=None):# self.name = name# self.girlfriend = girlfriend## def have_a_dinner(self):# if self.girlfriend:# print('%s 和 %s 一起共度晚餐'%(self.name,self.girlfriend.name))# else:# print('单身狗吃什么吃')## def append_girl(self,girl):# self.girlfriend = girl## def remove_girl(self):# self.girlfriend = None## class Girl:# def __init__(self, name):# self.name = name # 起初创业没有女朋友# b = Boy('alex')# # b.have_a_dinner()## # 突然有一天 alex 家 拆迁,有女朋友了# g = Girl('如花')# # print(g.name)# b.append_girl(g)# b.have_a_dinner() # wusir 娃娃亲# gg = Girl('金莲') ## wu = Boy('武大',gg)# wu.have_a_dinner() # 西门太白出现。# gg = Girl('金莲') ## wu = Boy('武大',gg)# # 女朋友失去了# wu.remove_girl()# wu.have_a_dinner() # 学校,老师# 老师一定要关联学校# class School:# def __init__(self,name,address):# self.name = name# self.address = address### class Teacher:# def __init__(self, name,comment,school):# self.name = name# self.comment = comment# self.school = school## def check_address(self):# print('%s 在%s 办公'%(self.name,self.school.address))### beijing = School('老男孩北京校区','美丽富饶的沙河')# shanghai = School('老男孩上海校区','上海漳江新区')# shenzhen = School('老男孩深圳校区','南山区平山村儿')## t1 = Teacher('太白','优秀',beijing)# t2 = Teacher('海峰','优秀',shanghai)# t3 = Teacher('日天','看吧',shenzhen)# t1.check_address()# 学校同时也要关乎多个老师# class School:# def __init__(self,name,address):# self.name = name# self.address = address# self.teacher_list = [] # def append_teacher(self,teacher): # self.teacher_list.append(teacher) # class Teacher:# def __init__(self, name,comment,school):# self.name = name# self.comment = comment# self.school = school## def check_address(self):# print('%s 在%s 办公'%(self.name,self.school.address)) # beijing = School('老男孩北京校区','美丽富饶的沙河')# shanghai = School('老男孩上海校区','上海漳江新区')# shenzhen = School('老男孩深圳校区','南山区平山村儿') # t1 = Teacher('太白','优秀',beijing)# t2 = Teacher('景女神','优秀',beijing)# t3 = Teacher('文周','优秀',beijing)# # t2 = Teacher('海峰','优秀',shanghai)# # t3 = Teacher('日天','看吧',shenzhen)# # t1.check_address()# beijing.append_teacher(t1)# beijing.append_teacher(t2)# beijing.append_teacher(t3) # # print(beijing.teacher_list)# for i in beijing.teacher_list:# print(i.name)
# 04 面向对象三大特征:继承2
# 单继承的练习题:# 1# class Base:# def __init__(self, num):# self.num = num# def func1(self):# print(self)# print(self.num)## class Foo(Base):# pass## obj = Foo(123)# print(obj)# obj.func1() # 2# class Base:## def __init__(self, num): # self = obj num = 123# self.num = num # 给对象空间封装属性 num = 123## def func1(self):# print(self.num)## class Foo(Base):## def func1(self):# print("Foo. func1", self.num)# obj = Foo(123) # 实例化对象,产生obj# obj.func1() # 3 # class Base:## def __init__(self, num): # self= obj num = 123# self.num = num # obj.num = 123## def func1(self):# print(self.num)## class Foo(Base):## def func1(self):# print("Foo. func1", self.num)## obj = Foo(123) # obj 执行__init__# obj.func1() # 4# class Base:## def __init__(self, num):# self.num = num # obj.num = 123## def func1(self):# print(self.num) # 123# self.func2() # "Foo.func2"## def func2(self):# print("Base.func2")## class Foo(Base):# def func2(self):# print("Foo.func2")## obj = Foo(123) # obj 执行__init__# obj.func1() # 5 # 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# 第一次循环:Base(1) self.num = 1 Base(1).func2()# 第一次循环:Base(2) self.num = 2 Base(2).func2()# 第一次循环:Foo(3) self.num = 3 Foo(3).func2() # class Base:## def __init__(self, num):# self.num = num## def func1(self):# print(self.num) # 3# 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() # 第一次:obj = Base(1) obj.num = 1 1 111 1# 第二次:obj = Base(2) obj.num = 2 2 111 2# 第三次:obj = Foo(3) obj.num = 3 3 222 3 # class A:# def __init__(self):# self.func1()## def func1(self):# print('in A func1')## class B(A):# def func1(self):# print('in B func1')## obj = B()# obj.func1() # 两个 in B func1 # 一个 in B func1 # 多继承'''python2x版本: python2.2版本之前,类都是经典类,不继承object python2.2之后,出现了新式类: class A(object) class A(object) 新式类 class B: 经典类 python3x :默认都继承object 都是新式类。 经典类: 多继承,深度优先。新式类: c3算法。'''# class A(object):# pass # class Immortal:# """# 神仙类# """# def __init__(self):# self.age = '长生不老'## def fly(self):# print('神仙都会飞')## def eat(self):# print('神仙也需要进食')## class Monkey:## def climb(self):# print('猴子都会爬树')## def eat(self):# print('猴子需要进食')### class Monkey_sun(Immortal,Monkey):# pass## sun = Monkey_sun()# # sun.fly()# # sun.climb()# sun.eat() # 经典类: 多继承,深度优先。 # class A:# pass# class B(A):# pass# class C(A):# pass# class D(B, C):# pass# class E:# pass# class F(D, E):# pass# class G(F, D):# pass# class H:# pass# class Foo(H, G):# pass # 经典类遵循深度优先。从左至右依次寻找。 # 新式类:# class A:# pass## class B:# pass## class C(B,A):# pass# #mro(Child(Base1,Base2)) = [ Child ] + merge( mro(Base1), mro(Base2), [ Base1, Base2] )# # mro(C) = mro(C(B,A)) = [C] + merge(mro(B),mro(A),[B,A])## # [E,D]# class D:# pass## class E(D):# pass # 表头 表尾# 表头 列表的第一个元素# 表尾 列表中表头以外的元素集合(可以为空)# [A,B,D,C] A表头 BDC 表尾 # merge( [E,O], [C,E,F,O], [C] ) # 取出第一个列表的表头 E 所有列表的表尾 O E F O ''# 如果E 在所有的表尾中,那么就跳过这个列表# merge( [E,O], [C,E,F,O], [C] )# 第二个列表表头: C O EFO '' 将C 单独提出# [c] + merge([E,O], [E,F,O])# mro(C) = mro(C(B,A)) = [C] + merge(mro(B),mro(A),[B,A])# mro(A) = mro(A(B,C))"""mro(A(B,C)) = [A] + merge(mro(B),mro(C),[B,C])mro(B) = mro(B(D,E))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) = mro(C(E,F))mro(C(E,F)) = [C] + merge([E,O],[F,O],[E,F])mro(C(E,F)) = [C,E] + merge([O],[F,O],[F])mro(C(E,F)) = [C,E,F] + merge([O],[O])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] """ # class O:# def func(self):# print('in O')# class D(O):# pass# # def func(self):# # print('in D')## class E(O):# pass# # def func(self):# # print('in E')## class F(O):# def func(self):# print('in F')## class B(D,E):# pass# # def func(self):# # print('in B')## class C(E,F):# pass# # def func(self):# # print('in C')## class A(B,C):# def func(self):# super().func()# print('in A')# obj = A()# obj.func()# mro(A(B,C)) = [A,B,D,C,E,F,O]# print(A.__mro__)# super 遵循mro 顺序
# 依赖关系# 大象走进冰箱,并关闭# 基本类# class Elphant:# def __init__(self,name):# self.name = name# def open(self):# """# 开门# :return:# """# pass# def close(self):# """# 关门# :return:# """# pass# # class Refrigerator:# def __init__(self,name):# self.name = name# # def open_door(self):# print('冰箱门被打开啦')# # def close_door(self):# print('冰箱门被关闭啦')
# 接下来# class Elphant:# def __init__(self, name):# self.name = name## def open(self,obj):# """# 开门# :return:# """# print('心里默念:1,2,3,开!')# obj.open_door()## def close(self):# """# 关门# :return:# """# print('心里默念:3,2,1关!')### class Refrigerator:# def __init__(self, name):# self.name = name## def open_door(self):# print('冰箱门被打开啦')## def close_door(self):# print('冰箱门被关闭啦')## e1 = Elphant('神奇的大象')# media = Refrigerator('美的')# e1.open(media)# 依赖关系:给一个类的方法传了一个参数此参数是另一个类的对象(类名)。# 这种依赖关系是所有关系中紧密型最低的,耦合性最低的。# 冰箱依赖大象,就好比企业与兼职工的关系,你中有我,我中没有你。
# 关联关系 聚合关系 组合关系
# class Boy:# def __init__(self, name, girlfriend=None):# self.name = name# self.girlfriend = girlfriend## def have_a_dinner(self):# if self.girlfriend:# print('%s 和 %s 一起共度晚餐'%(self.name,self.girlfriend.name))# else:# print('单身狗吃什么吃')## def append_girl(self,girl):# self.girlfriend = girl## def remove_girl(self):# self.girlfriend = None## class Girl:# def __init__(self, name):# self.name = name
# 起初创业没有女朋友# b = Boy('alex')# # b.have_a_dinner()## # 突然有一天 alex 家 拆迁,有女朋友了# g = Girl('如花')# # print(g.name)# b.append_girl(g)# b.have_a_dinner()
# wusir 娃娃亲# gg = Girl('金莲') ## wu = Boy('武大',gg)# wu.have_a_dinner()
# 西门太白出现。# gg = Girl('金莲') ## wu = Boy('武大',gg)# # 女朋友失去了# wu.remove_girl()# wu.have_a_dinner()
# 学校,老师# 老师一定要关联学校# class School:# def __init__(self,name,address):# self.name = name# self.address = address### class Teacher:# def __init__(self, name,comment,school):# self.name = name# self.comment = comment# self.school = school## def check_address(self):# print('%s 在%s 办公'%(self.name,self.school.address))### beijing = School('老男孩北京校区','美丽富饶的沙河')# shanghai = School('老男孩上海校区','上海漳江新区')# shenzhen = School('老男孩深圳校区','南山区平山村儿')## t1 = Teacher('太白','优秀',beijing)# t2 = Teacher('海峰','优秀',shanghai)# t3 = Teacher('日天','看吧',shenzhen)# t1.check_address()# 学校同时也要关乎多个老师class School: def __init__(self,name,address): self.name = name self.address = address self.teacher_list = []
def append_teacher(self,teacher): self.teacher_list.append(teacher)
class Teacher: def __init__(self, name,comment,school): self.name = name self.comment = comment self.school = school
def check_address(self): print('%s 在%s 办公'%(self.name,self.school.address))
beijing = School('老男孩北京校区','美丽富饶的沙河')shanghai = School('老男孩上海校区','上海漳江新区')shenzhen = School('老男孩深圳校区','南山区平山村儿')
t1 = Teacher('太白','优秀',beijing)t2 = Teacher('景女神','优秀',beijing)t3 = Teacher('文周','优秀',beijing)# t2 = Teacher('海峰','优秀',shanghai)# t3 = Teacher('日天','看吧',shenzhen)# t1.check_address()beijing.append_teacher(t1)beijing.append_teacher(t2)beijing.append_teacher(t3)
# print(beijing.teacher_list)for i in beijing.teacher_list: print(i.name)