CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day16-20/code/example17.py
Views: 729
1
"""
2
多重继承 - 一个类有两个或者两个以上的父类
3
MRO - 方法解析顺序 - Method Resolution Order
4
当出现菱形继承(钻石继承)的时候,子类到底继承哪个父类的方法
5
Python 2.x - 深度优先搜索
6
Python 3.x - C3算法 - 类似于广度优先搜索
7
"""
8
class A():
9
10
def say_hello(self):
11
print('Hello, A')
12
13
14
class B(A):
15
pass
16
17
18
class C(A):
19
20
def say_hello(self):
21
print('Hello, C')
22
23
24
class D(B, C):
25
pass
26
27
28
class SetOnceMappingMixin():
29
"""自定义混入类"""
30
__slots__ = ()
31
32
def __setitem__(self, key, value):
33
if key in self:
34
raise KeyError(str(key) + ' already set')
35
return super().__setitem__(key, value)
36
37
38
class SetOnceDict(SetOnceMappingMixin, dict):
39
"""自定义字典"""
40
pass
41
42
43
def main():
44
print(D.mro())
45
# print(D.__mro__)
46
D().say_hello()
47
print(SetOnceDict.__mro__)
48
my_dict= SetOnceDict()
49
my_dict['username'] = 'jackfrued'
50
my_dict['username'] = 'hellokitty'
51
52
53
if __name__ == '__main__':
54
main()
55
56