Python單繼承的使用 豐富老王開槍例子

2021-10-23 04:08:13 字數 2827 閱讀 1167

class

ren:

def__init__

(self)

: self.xueliang=

100 self.qiang=

none

def__str__

(self)

: msg=

"當前血量為:{}"

.format

(self.xueliang)

return msg

defanzidan

(self,danjia,zidan)

: danjia.baocunzidan(zidan)

defandanjia

(self,qiang,danjia)

: qiang.lianjiedanjia(danjia)

defnaqiang

(self,qiang)

:if self.qiang:

print

("手裡已有槍"

)else

: self.qiang=qiang

defkaiqiang

(self,diren)

: self.qiang.she(diren)

defdiaoxue

(self,weili)

: self.xueliang-=weili

class

qiang

:def

__init__

(self)

: self.danjia=

none

def__str__

(self)

:if self.danjia:

msg=

"當前槍中有彈夾"

else

: msg=

"當前槍中沒有彈夾"

return msg

deflianjiedanjia

(self,danjia)

:if self.danjia:

print

("槍中已有彈夾!"

)else

: self.danjia=danjia

defshe(self,diren)

: zidan=self.danjia.tanchuzidan(

)if zidan:

zidan.shanghai(diren)

class

sandan

(qiang)

:def

she(self,diren)

:for i in

range(3

):zidan=self.danjia.tanchuzidan(

)if zidan:

zidan.shanghai(diren)

else

:break

#super().she(diren)

class

danjia

:def

__init__

(self,rongliang)

: self.rongliang=rongliang

self.rongna=

def__str__

(self)

: msg=

"當前彈夾中有子彈:{}/{}"

.format

(len

(self.rongna)

,self.rongliang)

return msg

defbaocunzidan

(self,zidan)

:if self.rongliang-

len(self.rongna)

>0:

else

:print

("彈夾已滿!"

)def

tanchuzidan

(self):if

len(self.rongna)

>0:

zidan=self.rongna[-1

] self.rongna.pop(

)return zidan

else

:print

("目前彈夾中沒有子彈"

)class

zidan

:def

__init__

(self,weili)

: self.weili=weili

def__str__

(self)

: msg=

"該子彈的威力為:{}"

.format

(self.weili)

return msg

defshanghai

(self,diren)

: diren.diaoxue(self.weili)

laowang=ren(

)qiang=sandan(

)danjia=danjia(20)

zidan=zidan(5)

laowang.anzidan(danjia,zidan)

laowang.andanjia(qiang,danjia)

laowang.naqiang(qiang)

diren=ren(

)laowang.kaiqiang(diren)

print

(diren)

#修復漏洞是乙個漫長而不斷完善的過程,切忌急於求成

python 單繼承,多繼承

父類 class person def init self,name,age self.name name self.self.age age def introduce self print 父類方法person self.name 學生類 繼承 person class student pers...

python中的單繼承

一 介紹 python 同樣支援類的繼承,如果一種語言不支援繼承,類就沒有什麼意義。派生類的定義如下所示 class derivedclassname baseclassname1 需要注意圓括號中基類的順序,若是基類中有相同的方法名,而在子類使用時未指定,python從左至右搜尋 即方法在子類中未...

python中的單繼承

所謂的單繼承就是 個 類只有 個 類。類會繼承 類所有的屬性和 法。通過單繼承,子類可以獲取父類所有的成員,也可以自己新增新的成員,同時還可以改寫繼承自父類的方法。例如,class playgame def init self,gamename,gametype,gamescore,player s...