關於python匯入類的自言自語

2021-10-02 23:16:37 字數 2294 閱讀 3850

今天學習了一下python匯入類的內容,自己以這篇blog作為記錄學習的乙個收穫吧~

同屬目錄下有car.py與my_car.py兩個檔案,car.py內容如下

#car.py

class

car():

def__init__

(self,make,model,year)

: self.make=make

self.model=model

self.year=year

self.odometer=

0def

get_description_name

(self)

: long_name=

str(self.year)

+' '

+self.make+

' '+self.model

return long_name

defread_odometer

(self)

:print

('your car has '

+self.odometer+

' miles on it'

)def

update_odometer

(self,data)

:if data>self.odometer:

self.odometer=data

print

("the odometer is updating now."

)elif data<=self.odometer:

print

("please check the data you input"

)def

increment_odometer

(self)

: self.odometer+=

5class

battery()

:def

__init__

(self,battery_size)

: self.battery_size=battery_size

defupdate_battery

(self)

:if self.battery_size>=50:

print

("your battery size is big enough"

)else

:print

('your battery size is updating now.'

) self.battery_size=

50print

("your battery just finished the update."

)class

electriccar

(car)

:def

__init__

(self,make,model,year)

:super()

.__init__(make,model,year)

self.battery=battery(30)

defcheck_the_battery

(self)

:if self.battery.battery_size>=50:

print

("the battery's capacity is strong enough"

)else

:print

("your battery need an update now."

)

mycar.py檔案如下:

from car import electriccar

my_electric_car=electriccar(

'tesla'

,'s3'

,2016

)print

(my_electric_car.get_description_name())

my_electric_car.check_the_battery(

)my_electric_car.battery.update_battery(

)my_electric_car.check_the_battery(

)

其中,在car.py中,battery這個類在electriccar的初始化中也被包含在內。而mycar.py去匯入類時,只需要匯入electriccar即可,而不需要匯入battery這個類。

同理,當匯入類設計到父類和子類時,只需要匯入你所需要的子類即可,父類不需要單獨匯入。

關於ArrayList和Vector的自動增長

集合中常用的兩個類,arraylist和vector是兩個先進先出的堆疊。他們乙個是非同步,乙個是同步。new arraylist 或new vector 預設都是10個長度,vector構造器中也可以構造乙個適當的步長,如 vector int initialcapacity,int capaci...

關於自增符的運算

首先,自增分為前置和後置 i和i 在單獨成式的情況下,兩者並沒有什麼區別,都是把i 1的值賦給i 但在表示式中,兩者有明顯的區別。例如int i 1,j 1 int a,b a i b j 執行結果 a 1,b 2,i 2,j 2 可以看出,後置自增先以i的值完成表示式的運算再使i 1 前置自增先使...

關於oracle的自增 sequence

在oracle中sequence就是所謂的序列號,每次取的時候它會自動增加,一般用在需要按序列號排序的地方。1 create sequence 你首先要有create sequence或者create any sequence許可權,create sequence emp sequence incr...