[英]Python creating multiple instances for a single object/class
我正在使用Python。 我已經閱讀了一些有關此內容的內容,似乎無法將其包裹住。 我想做的是創建一個名為Potions的類,其中包含各種葯水對象。 現在有一個葯水,一個簡單的HealthPotion。 我希望葯水可以堆疊在庫存和商店庫存中。 因此,我需要一個庫存的葯水量實例以及每個攜帶葯水的商店庫存的實例。 葯水的數量是動態的,用於葯水的購買/出售和搶劫。 如果有人可以提供基本的解釋或示例,那將很好。
這是我所擁有的片段:
class Potion(Item):
def __init__(self, name, desc, val, amt, type, effect, bound):
Item.__init__(self, name, desc, val, amt, type, effect, bound)
self.name = name
self.desc = desc
self.val = val
self.amt = amt
self.type = 0 #Restorative
self.effect = effect
def use(self, you):
#Use health potion
you.hp_current += self.effect
you.displayStats()
#Format: Name, Description, Value, Amount, Type, Effect, Bound
HealthPotion = Potion('Health Potion', 'Restores 10 hit points when consumed', 10, 0,
0, 10, 0)
理想情況下,默認數量應設置為0,這樣我就可以宣布某家商店的庫存量為多少。 庫存和商店庫存被設置為一個數組,在該數組中添加和刪除項目。 我想我對這如何工作缺乏邏輯,我只是在舉例說明金額上遇到了麻煩。
編輯:這是我擁有的一種購買方法的一部分,以查看不使用實例就會發生什么。 這很丑陋,我發現you.inventory.y.amt無法正常工作。 y是從“商店”中顯示的項目列表中選擇的項目。
x = selection - 1 #Item menu starts at 1. But arrays start at 0. So it takes user input and subtracts 1 to match the array.
y = self.stock[x]
if y.val <= you.coin:
if y.amt == 0:
you.inventory.append(y)
you.inventory.y.amt += 1
else:
you.inventory.y.amt += 1;
you.coin -= y.val
self.funds += y.val
if self.stock.y.amt > 1:
self.stock.y.amt -= 1
else:
self.stock.y.amt -= 1
self.stock.pop(x)
我看過這樣的例子:
class foo:
a = 1
i = foo()
foo.a => 1
i.a => 1
i.a = "inst"
foo.a => 1
i.a => "inst"
我想知道是否我不只是創建第二個HealthPotion對象,但這聽起來不對。 這個例子使我另想。 也許我只是不了解實例化。
“這將創建Employee類的第一個對象”
emp1 = Employee("Zara", 2000)
“這將創建Employee類的第二個對象”
emp2 = Employee("Manni", 5000)
謝謝!
我認為您可能對類和實例的工作原理有些誤解。 如果您改為考慮別人,這可能會更有意義。
假設我們要在類層次結構中對人建模。 現在,一個人必須有一個名字,如果您要求他們說,他們會說出他們的名字:
class Person(object):
def __init__(self, name):
self.name = name
def speak(self):
print "Hi! My name is {self.name}.".format(self=self)
那么Person
一個實例就是一個person! 例如:
>>> basil = Person("Basil")
>>> polly = Person("Polly")
>>>
>>> basil.speak()
Hi! My name is Basil.
>>> polly.speak()
Hi! My name is Polly.
>>> basil == polly
False
因此,實例不是一種人,它實際上只是一個人。
我聽不到你問的,你不能有一個實例本身就是類的類嗎? 是的,當然可以! 它稱為元類,在某些情況下是非常強大的工具。 但是,這些不是。
現在,如果您看一下自己的情況,您看到了區別嗎? HealthPotion
不是一種特殊的葯水(比如說,這是我的口袋里的葯水),它是一種葯水。 我們通過類繼承來表達這種關系:定義一個新類HealthPotion
,該類繼承自Potion
。 然后,您可以擁有這些實例(在我的口袋里,在商店中的任何地方)。 如果要使用一種葯水,則可以使用一種特定的葯水,即該類的一個實例。
您那里的代碼使用了非常令人困惑的命名約定,我認為這使您感到困惑-HealthPotion不是一個類,而是一個實例,但是CamelCase名稱表明它是python中的一個類。
要使用您的葯水類來使用多種健康葯水,只需
health_potion_1 = Potion("Health Potion", ...)
health_potion_2 = Potion("Health Potion", ...)
foobar_potion_1 = Potion("Foobar Potion", ...)
#...
盡管這是非常糟糕的樣式,但您可能想要的是擁有一種方法來輕松創建具有相似特性的,具有不同效果的葯水和相似的葯水
為此,您應該
class HealthPotion(Potion):
def __init__(self, name="Health Potion", effect=10):
super(HealthPotion, self).__init__(name, "Restores %d points of health" % (effect, ), effect, 0, 0)
def use(self, you):
you.hp_current+=self.effect
如果要在一個清單中包含多個項目,則最簡單的方法是簡單地為清單創建一個清單(或集合或某個集合),並在清單中包含多個實例,例如
inventory = [HealthPotion(), HealthPotion()]
如果您想進行堆疊,我仍然認為這是庫存的功能,而不是物品的功能(除了item.stackable
成員之外),因此我將擁有一個Inventory
類來處理對象的集合,例如人員的內容,胸部或商店。 一個簡單的實現就是包裝
inventory = [(HealthPotion(), 2)]
其中任何相同的項目都表示為一對項目和金額
另外,如果您有stacks_with
方法,則很容易將前者轉換為后者:
def stack_size(item, inv):
"The number of items that will stack with item in inv"
return len([i for i in inv if item.stacks_with(i)])
def stacked_inventory(inv):
# if there is no stackable pair in the first i items
# (i.e. the stack size for that item is 0 in inv[0:i]),
# then the 'i'th item is a new stack,
# with stack_size(inv[i], inv) items in it
stacks = [ (inv[i], stack_size(inv[i]))
for i in range(0,len(inv))
if not stack_size(inv[i], inv[0:i])]
return stacks
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.