咱們在編程的過程當中,並不是都是要重頭開始。好比其餘人已經有現成的類,咱們可使用其餘找人編寫的類。術語稱之爲: 繼承。編程
當一個類繼承例外一個類時,它能夠得到這個類的全部屬性和方法:原有的類稱之爲 父類,新的類稱之爲子類。子類能夠繼承父類的全部方法和屬性,還能夠自定一些本身的方法和屬性。dom
好比咱們已經有了一個叫汽車的父類,咱們能夠繼承這個類,生成一個電動車的子類: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.") 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.") '''繼承car,生成一個新類''' class ElectricCar(Car): def __init__(self, make, model, year): super().__init__(make, model, year) my_BYD = ElectricCar("BYD", "Tang", 2019) print(my_BYD.get_description_name()) ''' 輸出: 2019 Byd Tang '''
經過上面的代碼,咱們看到,咱們基於一個car的父類,生成了一個ElectricCar的子類。code
在類定義是,在括號裏面包含父類的名稱,來表示繼承這個類: class NewClass(SupperClass)。對象
而真正繼承父類的方法和屬性的,則是在__init__方法中的super()方法的使用,該方法告訴Python使用父類的__init__方法,來從新構造一個類。blog
經過上面的例子,咱們能夠看到,子類能夠正確的調用父類的方法,實際上這時已是子類的方法了。繼承
咱們也能夠根據累的特性,給子類定義本身特有的屬性和方法:ip
好比電動車有一個電瓶,而且有方法能夠實時的顯示當前的電量。utf-8
#-*- 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.") '''繼承car,生成一個新類''' class ElectricCar(Car): def __init__(self, make, model, year): super().__init__(make, model, year) self.battery_size = 100 def describe_battery(self): print("Catr has " + str(self.battery_size) + "-kwh battery. " ) my_BYD = ElectricCar("BYD", "Tang", 2019) print(my_BYD.get_description_name()) my_BYD.describe_battery() ''' 輸出: 2019 Byd Tang Catr has 100-kwh battery. '''
在上述代碼中,咱們能夠看到,咱們在__init__方法中,添加了一個電瓶容量的屬性,get
self.battery_size = 100
而且添加了一個電動車特有的顯示電量的方法。
def describe_battery(self):
print("Catr has " + str(self.battery_size) + "-kwh battery. " )
這些方法是屬於子類(ElectricCar)的,它可以正確的被運行。
當父類中的某些方法,並不適用子類的時候怎麼辦吶?咱們能夠在子類中從新定義該方法。
好比Car類中有加汽油的方法,而這對電動車並不適用,咱們能夠在子類中對這個方法進行覆蓋重寫。子類在調用這個方法時,將採用子類的定義:
#-*- 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.") def fill_gas(self): print("Car is filling gas.") '''繼承car,生成一個新類''' class ElectricCar(Car): def __init__(self, make, model, year): super().__init__(make, model, year) self.battery_size = 100 def describe_battery(self): print("Catr has " + str(self.battery_size) + "-kwh battery. " ) def fill_gas(self): print("Electric car no gas tank.") my_BYD = ElectricCar("BYD", "Tang", 2019) my_BYD.fill_gas() ''' 輸出: Electric car no gas tank. '''
咱們在編寫代碼時候,須要靈活的對類進行定義。在編程思想中,現實生活中的全部對象,均可以被定義成類。
咱們儘量多訂一些類,以簡化咱們的代碼長度,同時也變成程序代碼的維護和修改。
好比在上述例子中,咱們對電動車類增長了一個電池的屬性和相關的方法。其實咱們也能夠新建一個電池的類,將電池特有的屬性和方法獨立開來。這樣咱們能夠根據這個類生成各式各樣的實例:
#-*- 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.") def fill_gas(self): print("Car is filling gas.") '''生成一個電池類''' class Battery(): def __init__(self, size = 100): self.size = size def describe_battery(self): print("Battery has " + str(self.size) + "-kwh battery. " ) '''繼承car,生成一個新類''' class ElectricCar(Car): def __init__(self, make, model, year): super().__init__(make, model, year) self.battery = Battery() def fill_gas(self): print("Electric car no gas tank.") my_BYD = ElectricCar("BYD", "Tang", 2019) my_BYD.battery.describe_battery() ''' 輸出: Battery has 100-kwh battery. '''
我麼能夠看到咱們增長了一個電池類Battery(),該類有本身屬性 size和方法describe_battery。咱們在定義電動車時,增長了一個battery的屬性,這個屬性是一個baterry的實例,咱們能夠認爲該屬性其實是一個對象 object,咱們能夠操做和使用它的屬性和方法。
這樣作的好處就是,有關電池的屬性和方法的修改,能夠放在battery類中進行處理。EelctricCar類中,只關注與其相關的屬性和方法。好比咱們能夠添加一個電池能跑多少里程的方法,該方法與電池的容量相關:
#-*- 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.") def fill_gas(self): print("Car is filling gas.") '''生成一個電池類''' class Battery(): def __init__(self, size = 100): self.size = size def describe_battery(self): print("Battery has " + str(self.size) + "-kwh battery. " ) def show_range(self): print("Battery has " + str(self.size * 3) + " killmaters on full charge") '''繼承car,生成一個新類''' class ElectricCar(Car): def __init__(self, make, model, year): super().__init__(make, model, year) self.battery = Battery() def fill_gas(self): print("Electric car no gas tank.") my_BYD = ElectricCar("BYD", "Tang", 2019) my_BYD.battery.describe_battery() my_BYD.battery.show_range() my_BYD.battery.size = 200 my_BYD.battery.describe_battery() my_BYD.battery.show_range() ''' 輸出: Battery has 100-kwh battery. Battery has 300 killmaters on full charge Battery has 200-kwh battery. Battery has 600 killmaters on full charge '''