python-day17 类与类之间的关系

一. 类与类之间的依赖关系

  依赖关系就相当于兼职工,公司需要你就来,不需要也可以换其他人     依赖关系: 我用你但你不属于我

 1 #依赖关系
 2 # class Daxiang:
 3 #     def open(self,bx):
 4 #         print('大象高高兴兴走到了冰箱的面前')
 5 #         bx.kai()
 6 #         print('大象打开了冰箱门')
 7 #     def zhuang(self):
 8 #         print('大象走进了冰箱')
 9 #     def close(self,bx):
10 #         print('大象要关冰箱门了')
11 #         bx.guan()
12 # class BinXiang:
13 #     def kai(self):
14 #         print('我是冰箱,我会开门')
15 #     def guan(self):
16 #         print('我是冰箱,我会关门')
17 #
18 # ret = Daxiang()
19 # bx = BinXiang()
20 
21 # ret.open(bx)
22 # ret.zhuang()
23 # ret.close(bx)
View Code

二. 关联,组合,聚合关系

  关联关系  两种事物必须是互相关联的,但是在某些特殊情况下是可以更换或者更改的

        我需要你,你也属于我,这就是关联关系

  聚合关系  属于关联关系的一种特例===>各有各自的声明周期, 比如电脑,电脑里有CPU,硬盘,内存等等,

        电脑死机了,CPU还是好的,还是个完整的个体

  组合关系  属于关联关系的一种特例===> 组合关系比聚合关系更加紧密,比如大脑,心脏等重要器官组成一个人,

        如果人死了,这些器官也跟着死了

 1 # 关联关系
 2 # class Boy:
 3 #     def __init__(self,name,girlFriend = None):
 4 #         self.name = name
 5 #         self.girlFriend = girlFriend #关联关系
 6 #     def chi(self):
 7 #         if self.girlFriend:
 8 #             print("%s和%s一起吃饭" %(self.name,self.girlFriend.name))
 9 #         else:
10 #             print('爱丽丝都不在,吃什么吃')
11 # class Girl:
12 #     def __init__(self,name):
13 #         self.name = name
14 #     def happy(self):
15 #         print('刀剑使我快乐')
16 # b = Boy('桐木')
17 # a = Girl('爱丽丝')
18 # b.chi()
19 
20 # b对象的girlFriend赋值  g
21 # b.girlFriend = a   #突然天降女朋友
22 # b.chi()
23 # b.girlFriend = None
24 # b.chi()
View Code

三. 继承关系

  类名和对象默认是可以作为字典的key键的

  self在访问方法的顺序: 永远先找自己的,自己的找不到再找父类的

  self就是你访问方法的那个对象, 先找自己,然后再找父类的

四. 类的的特殊成员

1. 类名() 会⾃动执⾏__init__()

2. 对象() 会⾃动执⾏__call__()

3. 对象[key] 会⾃动执⾏__getitem__()

4. 对象[key] = value 会⾃动执⾏__setitem__()

5. del 对象[key] 会⾃动执⾏ __delitem__()

6. 对象+对象 会⾃动执⾏ __add__()

7. with 对象 as 变量量 会⾃动执⾏__enter__ 和__exit__

8. 打印对象的时候 会⾃动执⾏ __str__

9. ⼲干掉可哈希 __hash__ == None 对象就不可哈希了了.

 1 # 特殊成员
 2 # class Car:
 3 #     def __init__(self,color,pai): # 类名() 自动执行__init__()
 4 #         self.color = color
 5 #         self.pai = pai
 6 #     def __call__(self, *args, **kwargs): #对象() 自动执行__call__()
 7 #         print('证明来过')
 8 #     def __getitem__(self, item): #对象[key] 自动执行__getitem__()
 9 #         print('这是getitem,item = ',item)
10 #     def __setitem__(self, key, value):
11 #         print(key,value)
12 #     def __delitem__(self, key):
13 #         print(key)
14 #     def __add__(self, other): #对象+对象会自动执行
15 #         print("证明我来过")
16 #     def __enter__(self):
17 #         print('进来的时候')
18 #     def __exit__(self, exc_type, exc_val, exc_tb):
19 #         print('出去的时候')
20 #     def __str__(self):## 当前对象的字符串表示形式
21 #         return '111'
22 #     def __repr__(self):#  一个对象的官方字符串表示形式
23 #         return '我的车很厉害'
24 #     def __iter__(self):
25 #         return (i for i in range(10))
26 #     def __hash__(self):
27 #         __hash__ = None #干掉可哈希  __hash__ == None  对象就不可哈希了了.
28         # pass
29 
30 # ret = Car('red','6666') #执行__init__
31 # ret() #执行__call__()
32 # ret['key']#执行__getitem__()
33 # ret['abc'] = '123' #执⾏__setitem__()
34 # del ret['abc']#执行 __delitem__()
35 # b = Car('a','1')
36 # a = b+ret    #执行__add__()
37 # with ret as m:   #执行__enter__ 和__exit__
38 #     print('哈哈哈')
39 # print(ret)#执行 __str__
40 # print(repr(ret))#执行__repr__()
41 # for i in ret: # 执行__iter__
42 #     print(i)
43 # print(hash(ret)) #执行 __hash__()
44 
45 class Car:
46     def __init__(self, color, pai): # 初始化方法
47         print("哪有地呀")
48         self.color = color
49         self.pai = pai
50 
51     # 这里才是真正的构造方法
52     def __new__(cls, *args, **kwargs):
53         print("我的天哪")
54         # 固定的返回值
55         return object.__new__(cls)
56 
57 c = Car("红色", "京A66666") # 先执行__new__ 返回object.__new__(cls).把返回的空对象传递给 __init__()
58 
59 print(c.color)
View Code
原文地址:https://www.cnblogs.com/Thui/p/9931324.html