继承:子类继承父类,子类可以使用父类的属性和方法 交通工具类 class Vehicle: def __init__(self): self.name='' ''' def __init__(self,name): self.name = name; print('交通工具类被初始化 创建出来') ''' def run(self): print('交通工具在行驶....') def show(self): print('车的名字是%s'%self.name) class cixunfu: def run(self): print('在磁悬浮轨道上行驶..') 卡车,属性:载重,重写行驶的方法 class Truck(Vehicle): def __init__(self,name,weith): Vehicle.name=name; self.name = name; Vehicle.__init__(self,name); super(Truck,self).__init__(name); self.weith = weith; print('卡车被初始化 创建出来') 方法重写 def run(self): 父类.方法(self)或super().父类方法() super().show() super(Truck, self).show() Vehicle.show(self); print('%s卡车载重%d 吨在行驶....'%(self.name,self.weith)) :火车,属性:车箱个数,重写行驶的方法 class Trian(cixunfu,Vehicle): def __init__(self,name,nums): self.name=name; self.nums=nums def run(self): print('火车在轨道上呜呜的开...') class student: def goHome(self,vehicle): vehicle.run() print('学生在回家的途中...') class tui: def run(self): print('2条腿儿在行驶...') t=Truck('东风',15); t.run() c=Trian('京沪高铁',30); c.run(); ok=Trian('马航730',30); daizhong=tui(); xinpeng=student(); xinpeng.goHome(daizhong);