Heim > Fragen und Antworten > Hauptteil
from threading import Thread
class CountdownThread(Thread):
def __init__(self, n):
super(CountdownThread, self).__init__() #在继承Thread时,为什么要执行Thread的构造函数呢?
self.n = 0
def run(self):
while self.n > 0:
print('T-minus', self.n)
self.n -= 1
time.sleep(5)
c = CountdownThread(5)
c.start()
1.在继承Thread时,为什么要执行Tread的构造函数呢?
ps:本人背景自学+google,还请不吝赐教
大家讲道理2017-04-17 17:29:11
如果在子类里面重写了构造函数,那么就会调用子类的构造函数。如果没有就会调用其父类的构造函数。如果没有在子类的构造函数里面调用父类的构造函数。那么父类的构造函数是不会在子类里面调用的