-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple.py
58 lines (32 loc) · 1.09 KB
/
multiple.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 多重继承 一个子类继承多个父类
class Animal(object):
pass
# 大类:
class Mammal(Animal):
pass
class Bird(Animal):
pass
class Runnable(object):
def run(self):
print('Running...')
class Flyable(object):
def fly(self):
print('Flying...')
# 各种动物:
class Dog(Mammal, Runnable):
pass
class Bat(Mammal, Flyable):
pass
class Parrot(Bird):
pass
class Ostrich(Bird):
pass
# MixIn:Ostrich继承自Bird。但是,如果需要“混入”额外的功能,通过多重继承就可以实现,
# 比如,让Ostrich除了继承自Bird外,再同时继承Runnable。这种设计通常称之为MixIn。
# Python自带的很多库也使用了MixIn。举个例子,Python自带了TCPServer和UDPServer这两类网络服务,
# 而要同时服务多个用户就必须使用多进程或多线程模型,
# 这两种模型由ForkingMixIn和ThreadingMixIn提供。通过组合,我们就可以创造出合适的服务来。
class MyTCPServer(TCPServer, ForkingMixIn):
pass
class MyUDPServer(UDPServer, ThreadingMixIn):
pass