# 类与类之间的关系 # 依赖关系 # 大象走进冰箱并关门 class Elephant: def __init__(self, name): self.name = name def open(self, obj): # 给一个类的方法传了另一个类的对象 print("%s心里默念:1,2,3,开!" % self.name) obj.open_door() def close(self, obj1): print("%s心里默念:3,2,1,关!" % self.name) obj1.close_door() class Refrigerator: def __init__(self, name): self.name = name def open_door(self): print("%s冰箱门被大象打开了" % self.name) def close_door(self): print("%s冰箱门被大象关闭了" % self.name) e1 = Elephant("神奇的大象") haier = Refrigerator("海尔") e1.open(haier) e1.close(haier) # 心里默念:1,2,3,开! # 冰箱门被大象打开了 # 心里默念:3,2,1,关! # 冰箱门被大象关闭了 # 依赖关系:给一个类的方法传了一个参数,此参数是另一个类的对象(类名) # 这种依赖关系是所有关系中紧密型最低的,耦合性最低的 # 你中有我,我中没你
# 关联关系 class Boy: def __init__(self, name, girlfriend=None): self.name = name self.girlfriend = girlfriend def eat(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, girl): self.girlfriend = None class Girl: def __init__(self, name): self.name = name # 传一个参数表示 girlfriend 默认是 None b1 = Boy("alex") b1.eat() # 一个人不吃了 g1 = Girl("如花") b1.append_girl(g1) b1.eat() # alex和如花一起吃饭 g2 = Girl("金莲") b2 = Boy("武大", g2) b2.eat() # 武大和金莲一起吃饭 g3 = Girl("金莲") b3 = Boy("武大", g3) b3.remove_girl(g3) b3.eat() # 一个人不吃了 # 关联关系是你中有我中有你
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)) s1 = School("老男孩北京校区", "美丽富饶的沙河") s2 = School("老男孩上海校区", "上海漳江新区") s3 = School("老男孩深圳校区", "南山区平山村儿") t1 = Teacher("太白", "优秀", s1) t2 = Teacher("李白", "优秀", s2) t3 = Teacher("李黑", "秀", s3) t1.check_address() # 太白在美丽富饶的沙河教学 print(t1.school.address) # 美丽富饶的沙河 s1.append_teacher(t1) s1.append_teacher(t2) s1.append_teacher(t3) print(s1.teacher_list) # [<__main__.Teacher object at 0x00000159D17644E0>, # <__main__.Teacher object at 0x00000159D1764518>, # <__main__.Teacher object at 0x00000159D1764550>] for i in s1.teacher_list: print(i.name) # 太白 # 李白 # 李黑
# 单继承的练习 class Base: def __init__(self, num): self.num = num def func1(self): # 这里的 self 是 obj print(self) print(self.num) # obj.num class Foo(Base): pass obj = Foo(123) obj.func1() # 123 # 运⾏的是Base中的func1
class Base: def __init__(self, num): # self 就是 obj self.num = 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() # Foo. func1 123 运⾏的是Foo中的func1
class Base: def __init__(self, num): self.num = num def func1(self): # 这里的 self 就是 obj print(self.num) 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 func1是Base中的 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)] # Base(1) 是对父类Base实例化一个对象 for obj in lst: obj.func2() # 111 1 | 111 2 | 222 3 # 第一次循环:Base(1), 找Base类, self.num = 1 # 然后执行 Base(1)的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.func1() # 那笔来吧. 好好算 # 第一次: obj = Base(1), obj.num = 1, 然后执行Base中的 func1 # 此时,func1 中的 self.func2() 执行的是 Base 中的 func2() # 依次类推,注意,最重要的是要知道 obj 是属于哪个类的
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() # 还是那句话,最重要的是确定 obj 是哪个类的实例化对象!!!
# 多继承 class A: def __init__(self, name): self.name = name def func(self, w): self.w = w class B(A): pass b = B("wusir") # 注意这里的括号里必须加一个参数,因为b必须在__init__中执行,但B里面没有__init__,因此会在A中找