python程式設計題 繼承

2021-10-11 02:44:25 字數 2477 閱讀 1542

class

loancaculator()

:def

__init__

(self,loan,time)

: self.loan = loan

if time ==

'1':

self.time =

3elif time ==

'2':

self.time =

5elif time ==

'3':

self.time =

20def

get_total_interests

(self)

:return self.loan * self.get_interests_rate(

)def

get_interests_rate

(self)

:if self.time ==3:

return

0.0603

elif self.time ==5:

return

0.0612

elif self.time ==20:

return

0.0639

defget_monthly_payment

(self)

:return

(self.loan+ self.get_total_interests())

/(self.time *12)

loan =

int(

input

('請輸入貸款金額'))

time =

input

("請選擇貸款年限:1.3年(36個月) 2.5年(60個月) 3. 20年(240個月)"

)loan_caculate = loancaculator(loan,time)

print

('月供為:%f'

% loan_caculate.get_monthly_payment(

))

class

animal()

:def

__init__

(self,age)

: self.age = age

defprint_info

(self)

:print

('我今年%d歲了!'

%(self.age)

)class

bird

(animal)

:def

__init__

(self,color)

:super()

.__init__(4)

self.color = color

defprint_info

(self)

:print

('我是乙隻%s的鳥'

%(self.color)

)class

fish

(animal)

:def

__init__

(self,weight)

:super()

.__init__(2)

self.weight = weight

defprint_info

(self)

:print

('我是乙隻%d斤重的魚'

%(self.weight)

)super()

.print_info(

)bird = bird(

"紅色"

)bird.print_info(

)fish = fish(5)

fish.print_info(

)

class

phone

:def

call

(self)

:print

('使用功能機打**'

)class

iphone

(phone)

:def

call

(self)

:print

('使用蘋果手機打**'

)class

aphone

(phone)

:def

call

(self)

:print

('使用安卓手機打**'

)class

person()

:def

use_phone_call

(self,phone)

: phone.call(

)person = person(

)person.use_phone_call(phone())

person.use_phone_call(iphone())

person.use_phone_call(aphone(

))

python程式設計 單繼承

繼承的作用 貓類和狗類都是動物類,但是又各有區別。如果沒有繼承,那麼貓類和狗類就都需要建立各自的方法 如果它們都繼承自動物類,那麼很多共同的地方就只需要在動物類中定義一次即可,精簡了 繼承前 繼承前,各個類都需要自己定義自己的方法,不夠精簡 class animal def shout self p...

python程式設計題 python程式設計題庫

上期題目連線 1000道python題庫系列分享十一 9道 上期題目答案 本期題目 機器之心報道機器之心編輯部想要備戰 python 面試,這兩個專案有千道 python 問題與實現。之前機器之心介紹了 phd 大牛的求職之路,很多讀者感覺這位大牛太厲害了,他的經歷對我們幫助不大。對於一般的機器學習...

python物件導向程式設計 繼承

物件導向三大特性 封裝根據職責將屬性和方法封裝到乙個抽象的類中 繼承實現 的重用,相同的 不需要重複的編寫 多型不同的物件呼叫相同的方法,產生不同的執行結果,增加 的靈活度 1 繼承的語法class 類名 父類名 pass2 專業術語 3 繼承的傳遞性 子類擁有父類以及父類的父類中封裝的所有屬性和方...