python面向对象的三大特征--继承子类调用父类方法
#在子类中调用父类方法 class Vehicle: country="China" def __init__(self,name,speed,load,power): self.name=name self.speed=speed self.load=load self.power=power def run(self): print("开动啦") class Subway(Vehicle): def __init__(self,name,speed,load,power,line): Vehicle.__init__(self,name,speed,load,power) # self.name = name # self.speed = speed # self.load = load # self.power = power self.line=line def show_info(self): print(self.name,self.line) def run(self): Vehicle.run(self) print("%s开动了"%self.name) line13=Subway("成都","1000m/s",1000000000,"电",13) line13.show_info() line13.run()
#使用super方法调用父类方法
class Vehicle: country="China" def __init__(self,name,speed,load,power): self.name=name self.speed=speed self.load=load self.power=power def run(self): print("开动啦") class Subway(Vehicle): def __init__(self,name,speed,load,power,line): #Vehicle.__init__(self,name,speed,load,power) #super().__init__(name,speed,load,power) super(__class__,self).__init__(name,speed,load,power) super(Subway,self).__init__(name,speed,load,power) # self.name = name # self.speed = speed # self.load = load # self.power = power self.line=line def show_info(self): print(self.name,self.line) def run(self): # Vehicle.run(self) super().run() print("%s开动了"%self.name) line13=Subway("成都","1000m/s",1000000000,"电",13) line13.show_info() line13.run() print(line13.__class__)
如果我失败了,至少我尝试过,不会因为痛失机会而后悔