python面向对象 格式很重要
class Vehicle: def __int__(self,speed): self.speed=speed def drive(self,distance): print 'need %f hour(s)'%(distance/self.speed) class Bike(Vehicle): pass class Car(Vehicle): def __init__(self,speed,fuel): Vehicle.__int__(self,speed) self.fuel=fuel def drive(self,distance): Vehicle.drive(self,distance) print 'need %f fules'%(distance*self.fuel) b=Bike(15.0) c=Car(80.0,0.012) b.drive(100.0) c.drive(100.0)
class Vehicle: def __init__(self,sudu): self.sudu =sudu def drive(self,distance): print 'need %f hour(s)'%(distance/self.sudu) class Bike(Vehicle): pass class Car(Vehicle): def __init__(self, speed, fuel): Vehicle.__init__(self, speed) self.fuel = fuel def drive(self, distance): Vehicle.drive(self, distance) print 'need %f fuels' % (distance * self.fuel) b=Bike(10.0) b.drive(100.0) c=Car(80.0,0.012) c.drive(100.0)
不是格式问题 上面代码 init 写成了int 写代码一定要细心