Python學習筆記6

2021-10-09 14:55:33 字數 1615 閱讀 9159

class

car():

"""模擬汽車"""

def__init__

(self, brand, model, year)

:"""初始化汽車屬性"""

self.brand = brand

self.model = model

self.year = year

defget_main_information

(self)

:print

("品牌: 型號: 出場年份:"

.format

(self.brand, self.model,self.year)

)class

electricar

(car)

:"""模擬電動汽車,繼承自父類car"""

def__init__

(self, brand, model, year, bettery_size)

:super()

.__init__(brand, model, year)

#宣告繼承父類的屬性,不需要self

self.bettery_size = bettery_size #特有的屬性

defget_electric_quantit

(self)

:"""子類特有的方法,單獨寫"""

print

("電池容量為:{} kw.h"

.format

(self.bettery_size)

)my_ecar = electricar(

"ff91"

,"tomorrow"

,2048

,100

)my_ecar.get_main_information(

)#子類自動繼承父類全部方法

my_ecar.get_electric_quantit(

)

class

electriccar

(car)

:"""模擬電動汽車,繼承自父類car"""

def__init__

(self, brand, model, year, bettery_size)

:super()

.__init__(brand, model, year)

#宣告繼承父類的屬性

self.bettery_size = bettery_size #特有的屬性

defget_main_information

(self)

:"""使用相同的函式名,直接重寫"""

print

("品牌: 型號: 出場年份: 電池容量:"

.format

(self.brand, self.model,self.year,self.bettery_size)

)new_car = electriccar(

"ff92"

,"tomorrow2"

,2050

,120

)new_car.get_main_information(

)

比如將電池單獨封裝為乙個類,在電動汽車中建立乙個電池實體。

Python 學習筆記 6

6.1 字典 字典就是乙個關聯陣列 或者稱為雜湊表 它是通過關鍵字索引的物件的集合。使用大括號 來建立乙個字典。print 字典 dic print dic uu dic username dd dic home print uu print dd dic username pxl dic home...

Python學習筆記 6

python學習筆記 6 1 sequence sequence是一物件,乙個接乙個地儲存多種資料項。python中sequence有幾種不同型別。下面先看兩種sequence基本型別 字串和列表 在字串中訪問單個字元 用for迴圈迭代字串,語法如下 for variable in string s...

python 學習筆記(6)

我們要借助python的 語法,把decorator置於函式的定義處 log def now print 2015 3 25 答案 call now 2015 3 25 偏函式 python的functools模組提供了很多有用的功能,其中乙個就是偏函式 partial function funct...