[英]How to assign a class variable “dynamically” in Python?
我有以下代碼:
class Potion(object):
def __init__(self,name,var,varamount):
self.name=name
self.var=var
self.varamount=varamount
class Inventory(object):
def __init__(self):
self.items={}
def use_potion(self,potion):
potion.var+=potion.varamount
print("Used a ",potion.name," !")
class Player():
def __init__(self):
self.health=100
self.mana=100
self.stamina=100
inventory=Inventory()
player=Player()
healthpotion=Potion("Health potion",player.health,50)
inventory.use_potion(healthpotion)
在這里,我的健康葯水應該增加50到變量player.health
。 但是player.health
保持不變,只有healthpotion.var
被更改。 假設我想要不同類型的葯水(耐力,法力值,生命值),如何動態地將player.health
, player.stamina
和player.mana
分配給potion.var
?
這樣做不起作用的原因是您已經將參數player.health傳遞給了Potion,這與編寫相同:
Potion("Health potion",100,50)
class Potion():
def __init__(self,name,var,varamount):
self.name=name
self.var=var
self.varamount=varamount
class Inventory():
def __init__(self):
self.items={}
def add_item(self, item):
if item in self.items.keys():
self.items[item] += 1
else:
self.items[item] = 1
print ('Added a {} potion!'.format(item.name)
def remove_item(self, item):
if item in self.items.keys():
self.items[item] -= 1
return True
else:
print ('No such {} exists!'.format(item.name))
return False
class Player():
def __init__(self):
self.health=100
self.mana=100
self.stamina=100
self.inventory = Inventory()
def use_item(self, item):
if isinstance(item, Potion):
if self.inventory.remove_item(item):
if item.var == 'health':
self.health += item.varamount
print ('Used a {0} and healed {1}!'.format(item.name, item.varamount))
player=Player()
healthpotion=Potion("Health potion", 'health', 50)
player.use_item(healthpotion)
player.inventory.add_item(healthpotion)
player.use_item(healthpotion)
#No such Health potion exists!
#Added a health potion!
#Used a Health potion and healed 50!
您需要仔細考慮對象的工作方式。
庫存是使用葯水還是玩家使用葯水?
在提供的代碼中,我使Player類具有自己的清單。 然后播放器可以使用添加到其自身庫存中的葯水。
您需要發送創建的Player
實例,而不僅僅是其屬性值:
class Potion(object):
def __init__(self,name,var,varamount):
self.name=name
self.var=var
self.varamount=varamount
class Inventory(object):
def __init__(self):
self.items={}
def use_potion(self,potion):
# Your potion.var contains Player instance. Access players health.
potion.var.health += potion.varamount
print("Used a ",potion.name," !")
class Player():
def __init__(self):
self.health=100
self.mana=100
self.stamina=100
inventory=Inventory()
player=Player()
healthpotion=Potion("Health potion",player,50) # Send player instance.
print(player.health) # 100
inventory.use_potion(healthpotion)
print(player.health) # 150
您正在使用對象。 對象“知道”事物(其服裝的值),並且對象可以通過調用其方法來相互發送消息。
在這種情況下:
Player
知道他們的健康。 Potion
知道它能在多大程度上增進健康 Inventory
知道它是否有葯水。 Player
應該有一個清單 Player
可以選擇使用其庫存中的物品 Inventory
跟蹤是否已使用物料 基於這些事實,我們知道類需要哪些方法和屬性。
葯水需要知道可以恢復多少健康點
class Potion(object):
def __init__(self,name, varamount):
self.name=name
self.varamount=varamount
庫存需要能夠跟蹤其內容。
class Inventory(object):
def __init__(self):
self.items={}
def add_potion(self, potion):
self.items['potion'] = potion
def get_potion(self):
return self.items.get('potion')
def remove_potion(self):
self.items.pop('potion', None)
玩家需要能夠追蹤其健康狀況並使用葯水。
class Player():
def __init__(self, inventory):
self.health=100
self.mana=100
self.stamina=100
self.inventory = inventory
def use_potion(self):
potion = self.inventory.get_potion()
if potion:
self.health += potion.varamount
self.inventory.remove_potion()
print("Used a ",potion.name," !")
inventory=Inventory()
healthpotion=Potion("Health potion", 50)
inventory.add_potion(healthpotion)
player=Player(inventory)
player.use_potion()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.