python 类,子类,类创建实例,Python类练习 class Car(): ‘’‘汽车模拟’’’ def init(self,make,model,year): self.make= make self.model= model self.year=year self.odometer_reading= 100 def miaoshu(self): long_name = str(self.year) + ’ ’ + self.make + ’ ’ + self.model return long_name.title() def read_odometer(self):#汽车的里程数,也是最终汽车显示的里程数 print(“This car has " + str(self.odometer_reading) + " miles on it”) def update_odometer(self,mileage):#更改的里程数, if mileage>=self.odometer_reading: self.odometer_reading= mileage else:#you cuo wu print(“You can’t roll back an odomete!”)#you cuo wu def increment_odometer(self,miles):#增加的里程数 self.odometer_reading +=miles class Diandongche(Car):#添加子类电动车 def init(self,make,model,year): super().init(make,model,year)
my_car=Car(‘biyadi’,‘A4’,2002) print(my_car.miaoshu()) my_car.update_odometer(23500) my_car.read_odometer() my_car.increment_odometer(100) my_car.read_odometer() new_car=Diandongche(‘qq’,‘jiaoche’,2019) print(new_car.miaoshu()) new_car.update_odometer(20000) new_car.read_odometer() new_car.increment_odometer(2222) new_car.read_odometer()
—————————————————————————————————————————————————————— 2019.5.23
