當咱們熟悉和掌握了怎麼樣建立類和實例之後,咱們編程中的大多數工做都講關注在類的簡歷和實例對象使用,修改和維護上。編程
結合實例咱們來進一步的學習類和實例的使用:dom
咱們新建一個汽車的類:函數
#-*- coding:utf-8 -*- class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year def get_description_name(self): long_name = str(self.year) + ' ' + self.make + ' ' + self.model return long_name.title() my_car = Car("audi", "A4", 2016) print(my_car.get_description_name()) ''' 輸出: 2016 Audi A4 '''
經過上面的例子咱們能夠看到,咱們建立了一個car的類,這個類有三個參數,而且有一個打印全部信息的一個方法。學習
咱們知道其實每一個屬性都有初始的值,有些時候咱們的屬性並非爲空或者0,他須要一個默認的其餘值。咱們能夠在類的__init__方法中,對一些屬性進行初始化。一旦屬性具備初始值後,在實例化對象時,能夠不提供數值。優化
咱們在類中添加一個里程錶的屬性,他的初始值應該爲0,咱們添加一個方法用來讀取這個屬性。spa
#-*- coding:utf-8 -*- class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_description_name(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.") my_car = Car("audi", "A4", 2016) print(my_car.get_description_name()) my_car.read_odometer() ''' 輸出: 2016 Audi A4 This car has 0 miles on it. '''
咱們能夠直接修改實例的屬性,來實現我麼預想的操做:code
#-*- coding:utf-8 -*- class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_description_name(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.") my_car = Car("audi", "A4", 2016) print(my_car.get_description_name()) my_car.read_odometer() my_car.odometer_reading = 100 my_car.read_odometer() ''' 輸出: 2016 Audi A4 This car has 0 miles on it. This car has 100 miles on it. '''
也能夠經過添加特定方法來實現對實例屬性的修改:對象
#-*- coding:utf-8 -*- class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_description_name(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): self.odometer_reading = mileage my_car = Car("audi", "A4", 2016) print(my_car.get_description_name()) my_car.read_odometer() my_car.update_odometer(200) my_car.read_odometer() ''' 輸出: 2016 Audi A4 This car has 0 miles on it. This car has 200 miles on it. '''
我麼知道里程數通常都是遞增的,正常狀況下咱們是沒法將其往回調整的,因此咱們須要對update函數進一步的優化。輸入的參數必須大於等於當前值,小於則必須報錯:blog
#-*- coding:utf-8 -*- class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_description_name(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: print("You cannot do that.") my_car = Car("audi", "A4", 2016) print(my_car.get_description_name()) my_car.read_odometer() my_car.update_odometer(200) my_car.read_odometer() my_car.update_odometer(100) my_car.read_odometer() ''' 輸出: 2016 Audi A4 This car has 0 miles on it. This car has 200 miles on it. You cannot do that. This car has 200 miles on it. '''
根據現實中的狀況,其實咱們定義一個增量方法更爲合適,該方法每次都加上參數給定的值,而且該值應該大於等於0:ip
#-*- coding:utf-8 -*- class Car(): def __init__(self, make, model, year): self.make = make self.model = model self.year = year self.odometer_reading = 0 def get_description_name(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: print("You cannot do that.") def increase_odometer(self, miles): if miles >= 0: self.odometer_reading += miles else: print("The value is invalid, please input the number which should more than zero.") my_car = Car("audi", "A4", 2016) print(my_car.get_description_name()) my_car.read_odometer() my_car.update_odometer(200) my_car.read_odometer() my_car.increase_odometer(100) my_car.read_odometer() my_car.increase_odometer(-1) my_car.read_odometer() ''' 輸出: 2016 Audi A4 This car has 0 miles on it. This car has 200 miles on it. This car has 300 miles on it. The value is invalid, please input the number which should more than zero. This car has 300 miles on it. '''