标签:nim animal server def tcp 继承 span smi fork
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
通过多重继承,一个子类就可以同时获得多个父类的所有功能。
为了更好地看出继承关系,我们把Runnable
和Flyable
改为RunnableMixIn
和FlyableMixIn
。类似的,你还可以定义出肉食动物CarnivorousMixIn
和植食动物HerbivoresMixIn
,让某个动物同时拥有好几个MixIn:
class Dog(Mammal, RunnableMixIn, CarnivorousMixIn):
pass
比如,编写一个多进程模式的TCP服务,定义如下:
class MyTCPServer(TCPServer, ForkingMixIn):
pass
编写一个多线程模式的UDP服务,定义如下:
class MyUDPServer(UDPServer, ThreadingMixIn):
pass
如果你打算搞一个更先进的协程模型,可以编写一个CoroutineMixIn
:
class MyTCPServer(TCPServer, CoroutineMixIn):
pass
标签:nim animal server def tcp 继承 span smi fork
原文地址:https://www.cnblogs.com/soberkkk/p/12636169.html