python类的继承
class Bird: def __init__(self): self.eaten = False def eat(self): if(self.eaten == False): self.eaten = True print("Eating now") else: print("no,thinks") @staticmethod def fly(): print('''I believe I can fly, I believe i can hold the sky''') class SingBird(Bird): def __init__(self): super(SingBird,self).__init__() #必须加上__init__,否则解释器会提示Singbird没有eat的属性。 self.sound = "zzz,zzz" def sing(self): print(self.sound) singBird = SingBird() singBird.sing() singBird.eat() singBird.eat() singBird.fly()
运行的结果:
zzz,zzz
Eating now
no,thinks
I believe I can fly,
I believe i can hold the sky