# class Boy: # # def __init__(self, name, girlFriend=None): # # 在初始化的时候可以给一个对象的属性设置成另一个类的对象 # self.girlFriend = girlFriend # 一个男孩有一个女朋友 # # def chi(self): # if self.girlFriend: # print(f"带着他的女朋友{self.girlFriend.name}去吃饭") # else: # print("单身狗, 吃什么吃? 滚去学习.") # # def movie(self): # if self.girlFriend: # print(f"带着他的女朋友{self.girlFriend.name}去看电影") # else: # print("单身狗, 看什么看? 滚去学习.") # # # class Girl: # def __init__(self, name): # self.name = name # # b = Boy("宝浪") # g = Girl("孙艺珍") # b.chi() # # # alex给包浪介绍了一个女朋. 孙艺珍 # b.girlFriend = g # b.chi() # # g2 = Girl("梁咏琪") # b.girlFriend = g2 # 换了个女朋友 # b.chi() class School: def __init__(self, name): self.teach_list = [] # 这里要装多个老师 self.name = name def zhaopin(self, teach): self.teach_list.append(teach) def shangke(self): for t in self.teach_list: t.work() class Teacher: def __init__(self, name): self.name = name def work(self): print(f"{self.name}在上课") lnh = School("老男孩") t1 = Teacher("武sir") t2 = Teacher("太白") t3 = Teacher("哪吒") t4 = Teacher("女神") t5 = Teacher("日天") t6 = Teacher("宝浪") lnh.zhaopin(t1) lnh.zhaopin(t2) lnh.zhaopin(t3) lnh.zhaopin(t4) lnh.zhaopin(t5) lnh.zhaopin(t6) lnh.shangke()