class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer = 0 def get_descriptive(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() def read_odometer(self): print(str(self.odometer))1. 直接修改属性的值
my_new_car.odometer = 19 my_new_car.read_odometer()2. 通过方法修改属性的值
#内部增加一个方法来修改属性 ... def update_odometer(self, mileage): self.odometer = mileage ... my_new_car.update_odometer(19) my_new_car.read_odometer()3.通过方法对属性的值进行递增
# 内部增加一个方法来递增属性值 ... def increment_odometer(self, miles): self.odometer += miles ... my_used_car = Car('subaru', 'outback', 2013) my_used_car.update_odometer(23500) my_used_car.read_odometer() my_used_car.increment_odometer(100) my_used_car.read_odometer()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)