#在子类中调用父类的方法 class Vehicle1: 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('开动啦') print('开动啦') print('开动啦') print('开动啦') class Subway(Vehicle1):#子类不止只有父类这些数据属性 #可以重写init方法,也可以不写,写了数据属性就属于派生 def __init__(self,name,speed,load,power,line): # Vehicle.__init__(self,name,speed,load,power) super().__init__(name,speed,load,power)#如果不传参数相当于传入(子类,self) # super(Subway,self).__init__(name,speed,load,power) self.line=line def show_info(self): print(self.name,self.line,self.load,self.power) def run(self): # Vehicle.run(self) super().run() print('%s %s 线,开动了 '%(self.name,self.line)) line13=Subway('北京地铁','10km/s',100000,'电',13) line13.show_info() line13.run()
2018-09-06 14:19:58