溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Python類繼承機(jī)制是什么

發(fā)布時間:2020-09-24 10:59:11 來源:億速云 閱讀:119 作者:Leah 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)Python類繼承機(jī)制是什么,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

下面實現(xiàn)一個類繼承的小程序,下面一邊結(jié)合代碼一邊介紹相關(guān)繼承的知識。例子以車、汽車為例,車為父類、汽車為子類。

一、定義父類Vehicle

class Vehicle():
   def __init__(self,wheelcount, power):#構(gòu)造方法,參數(shù)有輪子數(shù)和動力
       self.wheelcount,self.power,self.totaldistance = wheelcount,power,0
       #初始化行駛總里程為0
   def gettotaldistance(self):return self.totaldistance  #定義獲取形式總里程的方法
   def drive(self,distance):#定義drive方法
           self.totaldistance += distance

二、定義子類Car

class Car(Vehicle):
   def __init__(self,wheelcount, power):
       super().__init__(wheelcount, power)
       Vehicle.__init__(self,wheelcount, power)
       super(Car,self).__init__(wheelcount, power)
       self.oil,self.oilcostperkm=0,0.1

子類內(nèi)首先重寫了構(gòu)造方法,注意:

1.首先調(diào)用了超類的構(gòu)造方法,為了說明超類方法的調(diào)用,代碼將三種調(diào)用超類構(gòu)造方法都實現(xiàn)了,實際上只要有一種就夠了;

2.超類構(gòu)造方法中初始化了輪子數(shù)、動力以及總行駛里程。子類調(diào)用超類構(gòu)造方法后,對于汽車又初始化了油量和每公里油耗。?

 def drive(self,distance):
       realdistance=min(distance,self.oil/self.oilcostperkm)
       super().drive(realdistance)
       self.oil -= realdistance*self.oilcostperkm
       print("車開了{(lán)}公里,目前郵箱存油{:.2f}升,目前車輛總里程:{}KM".format(realdistance,self.oil, 
       super().gettotaldistance())

子類重寫了父類的drive方法,本次只用了一種老猿推薦的方式調(diào)用父類的drive方法,重寫的方法內(nèi)根據(jù)油量確認(rèn)了實際駕駛里程之后調(diào)用了父類的drive方法,同時對油量進(jìn)行了調(diào)整,輸出了一些車況信息,其中調(diào)用了父類的gettotaldistance()方法。 

def  oiling(self,oil):
       self.oil+=oil
       print("加油{}升,目前郵箱存油{:.2f}升".format(oil,self.oil))

  實現(xiàn)子類獨有的加油方法,父類的車可以是畜力或人力等其他方式驅(qū)動就沒有這個方法。

def needoiling(self):
    if self.oil<5: return True
    else:return False

 實現(xiàn)子類獨有的是否需要加油判斷方法。   

def output(self):
    print("車子動力為{},100KM油耗{}升,車子累計行駛{}KM,油箱存油{:.2f}L".format(self.power,self.oilcostperkm*100, 
    super().gettotaldistance(),self.oil))

實現(xiàn)子類獨有的輸出車況的方法,其中調(diào)用了父類的gettotaldistance()方法。

到此為止整個子類的代碼實現(xiàn)完成,它完全繼承了父類方法gettotaldistance,采用重寫+父類調(diào)用方式實現(xiàn)了drive和構(gòu)造方法的繼承,并實現(xiàn)了needoiling、oiling兩個子類獨有的方法,其實例變量self.wheelcount,self.power,self.totaldistance是從父類繼承。

三、調(diào)用的代碼

下面是使用該類定義的一個實例,

car = Car(4,'汽油發(fā)動機(jī)')
car.oiling(50)
for i in range(1,100):
print("***************第{}次循環(huán)************".format(i))
car.oiling(random.randint(10,60)) ?#隨機(jī)加油x升
car.drive(random.randint(5,1000)) #隨機(jī)駕駛x公里
car.output() ?#輸出車況信息
if car.needoiling(): break ?#如果油不夠了就結(jié)束循環(huán)

四、    上述例子的完整源代碼

#coding:utf-8
import random
class Vehicle():
   def __init__(self,wheelcount, power):
       self.wheelcount,self.power,self.totaldistance = wheelcount,power,0
  
   def drive(self,distance):
       self.totaldistance += distance  
       
   def gettotaldistance(self):return self.totaldistance

class Car(Vehicle):
   def __init__(self,wheelcount, power):
       super().__init__(wheelcount, power)
       Vehicle.__init__(self,wheelcount, power)
       super(Car,self).__init__(wheelcount, power)
       self.totaldistance,self.oil,self.oilcostperkm=0,0,0.1
 
   def drive(self,distance):
       realdistance=min(distance,self.oil/self.oilcostperkm)
       super().drive(realdistance)
       self.oil -= realdistance*self.oilcostperkm
       print("車開了{(lán)}公里,目前郵箱存油{:.2f}升,目前車輛總里程:{}KM".format(realdistance,
       self.oil,super().gettotaldistance()))
           
   def  oiling(self,oil):
       self.oil+=oil
       print("加油{}升,目前郵箱存油{:.2f}升".format(oil,self.oil))
  
   def needoiling(self):
       if self.oil<5: return True
       else:return False
       
   def output(self):
       print("車子動力為{},100KM油耗{:.2f}升,車子累計行駛{}KM,油箱存油{:.2f}L".format(self.power,self.oilcostperkm*100,super().gettotaldistance(),self.oil))


car = Car(4,'汽油發(fā)動機(jī)')
car.oiling(50)
for i in range(1,100):
   print("***************第{}次循環(huán)************".format(i))
   car.oiling(random.randint(10,60)) 
   car.drive(random.randint(5,1000))
   car.output()
   if car.needoiling(): break

關(guān)于Python類繼承機(jī)制是什么就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI