重構:spa
import multiprocessing import time class ClockProcesses(multiprocessing.Process): def __init__(self,value): multiprocessing.Process.__init__(self) #若是不寫該語句,則新__init__()將會徹底覆蓋原來的定義, # super(ClockProcesses.self).__init__(self)#等同於上句功能 self.value = value def sum(self,a,b): return a + b def mul(self,a,b): return a*b def run(self):# 執行start()時,自動執行run語句 a,b = 1,2 if self.value == "+": print(self.sum(a,b)) else: print(self.mul(a,b)) p = ClockProcesses("*") p.start()
運行code