一、类与类之间的关系
1、依赖关系
1 class Elephant: 2 3 def __init__(self, name): 4 self.name = name 5 6 7 def open(self, ref): # 想要的是一个冰箱。 是哪个冰箱没有制定 8 print("冰箱哥哥, 开门把") 9 ref.open_door() 10 11 def close(self, ref): # 依赖关系 12 print("冰箱哥哥, 我进来了。 关门把") 13 ref.close_door() 14 15 def jin(self): 16 print("进冰箱装自己") 17 18 class Refrigerator: 19 20 def open_door(self): 21 print("冰箱陌陌的打开了自己的门") 22 def close_door(self): 23 print("冰箱陌陌的关上了自己的门 ") 24 25 # class GaoYaGuo: 26 # def open_door(self): 27 # print("冰箱陌陌的打开了自己的门") 28 # def close_door(self): 29 # print("冰箱陌陌的关上了自己的门 ") 30 31 32 alex = Elephant("李杰") 33 bx1 = Refrigerator() 34 35 # 36 alex.open(bx1) 37 alex.jin() 38 alex.close(bx1) 39 object
2、关联关系
1 # class Boy: 2 # def __init__(self, name, xingge, girlFriend=None): 3 # self.name = name 4 # self.xingge = xingge 5 # self.girlFriend = girlFriend 6 # 7 # def yujian(self, girl): 8 # self.girlFriend = girl 9 # 10 # def chi(self): 11 # if self.girlFriend: 12 # print("随便池! %s 和 %s" % (self.name, self.girlFriend.name)) 13 # else: 14 # print("单身狗, 池什么池?") 15 # 16 # class Girl: 17 # def __init__(self, name, boyFriend): 18 # self.name = name 19 # self.boyFriend = boyFriend 20 # def chi(self): 21 # print("%s在吃饭" % self.name) 22 # 23 # girl = Girl("白骨精") 24 # 25 # alex = Boy("金王", "娘") 26 # alex.chi() 27 # 28 # alex.yujian(girl) 29 # alex.chi() 30 # 31 # # 找到alex的女朋友 32 # # alex.girlFriend.name 33 # alex.girlFriend.chi() 34 35 36 37 # 一个对多个. 38 39 class School: 40 def __init__(self, name, address, phone): 41 self.name = name 42 self.address = address 43 self.phone = phone 44 self.__teach_list = [] 45 46 def zhaopin(self, t): 47 self.__teach_list.append(t) 48 49 def display(self): 50 for el in self.__teach_list: 51 print(el.name, el.hobby) 52 53 54 55 56 class Teacher: 57 def __init__(self, name, gender, salary, hobby, school): 58 self.name = name 59 self.gender = gender 60 self.salary = salary 61 self.hobby = hobby 62 63 self.school = school 64 65 oldboy_bj = School("北京老男孩", "美丽富饶的沙河", "10086") 66 oldboy_sh = School("北京老男孩, 上海分校", "上海浦东", "10010") 67 oldboy_sz = School("北京老男孩, 深圳分校(骑士计划)", "南山区", "10000") 68 69 70 t1 = Teacher("配齐", "男", 200000, "上课", oldboy_bj) 71 t2 = Teacher("太白", "男", 150000, "开车", oldboy_bj) 72 t3 = Teacher("Eggon", "男", 123456, "钻研技术", oldboy_sh) 73 t4 = Teacher("高鑫", "女", 45678, "相夫教子", oldboy_sz) 74 t5 = Teacher("日天", "男", 666, "看天", oldboy_sz) 75 76 77 # print(t3.school.address) # 找到老师所在的学校的地址 78 79 oldboy_bj.zhaopin(t1) 80 oldboy_bj.zhaopin(t2) 81 oldboy_bj.display() 82 83 oldboy_sh.zhaopin(t3) 84 85 oldboy_sz.zhaopin(t4) 86 oldboy_sz.zhaopin(t5) 87 88 oldboy_sz.display()
3、类名与对象是否可作为key
1 # 可哈希. 内部是否哈希算法 __hash__ 2 3 # class Foo(object): # 所有的类都会默认继承object 4 # def __init__(self): 5 # pass 6 # def func(self): 7 # pass 8 # __hash__ = None 9 # 10 # dic = {} 11 # dic[Foo] = "123456" # 类名是可哈希的。 12 # dic[Foo()] = "刘伟" # 类中是否包含__hash__ 13 # print(dic) 14 15 # 默认的类和对象都是可哈希的 16 17 # class Base: 18 # def __init__(self, num): 19 # self.num = num 20 # 21 # def func1(self): 22 # print(self.num) 23 # 24 # class Foo(Base): 25 # pass 26 # 27 # obj = Foo(123) 28 # obj.func1() # 123 29 30 # class Base: 31 # def __init__(self, num): 32 # self.num = num 33 # def func1(self): 34 # print(self.num) 35 # 36 # class Foo(Base): 37 # def func1(self): 38 # print("Foo. func1", self.num) 39 # 40 # obj = Foo(123) 41 # obj.func1() # ???? Foo. func1 123 42 43 # 44 # class Base: 45 # def __init__(self, num): 46 # self.num = num 47 # def func1(self): 48 # print(self.num) 49 # self.func2() 50 # def func2(self): 51 # print("Base.func2") 52 # 53 # class Foo(Base): 54 # def func2(self): 55 # print("Foo.func2") 56 # 57 # obj = Foo(123) 58 # obj.func1() # 123 59 60 # class Base: 61 # def __init__(self, num): 62 # self.num = num 63 # 64 # def func1(self): 65 # print(self.num) 66 # self.func2() 67 # 68 # def func2(self): 69 # print(111, self.num) 70 # 71 # class Foo(Base): 72 # def func2(self): 73 # print(222, self.num) 74 # 75 # lst = [Base(1), Base(2), Foo(3)] 76 # for obj in lst: 77 # obj.func2() 78 79 # class Base: 80 # def __init__(self, num): 81 # self.num = num 82 # 83 # def func1(self): 84 # print(self.num) 85 # self.func2() 86 # 87 # def func2(self): 88 # print(111, self.num) 89 # 90 # class Foo(Base): 91 # def func2(self): 92 # print(222, self.num) 93 # 94 # lst = [Base(1), Base(2), Foo(3)] 95 # for obj in lst: 96 # obj.func1() 97 98 # 1 99 # 111 1 100 # 2 101 # 111 2 102 # 3 103 # 222 3 104 105 # 总结: self当前访问xx方法的那个对象
来源:https://www.cnblogs.com/hq82/p/9721156.html