2011-11-10 216 views
2

繼承和默認值,我試圖與通用__init__值的一類,但有它的子類的默認值,像這樣:的Python:在__init__

class Enemy: 

def __init__(self, difficulty, power, MaxHP, magic, MaxMP, speed, name): 
    self.power = power + 2*difficulty 
    self.HP = self.MaxHP = MaxHP + 5*difficulty 
    self.magic = magic + 2* difficulty 
    self.MP = self.MaxMP = MaxMP + 5*difficulty 
class Goblin(Enemy): 
def __init_(self, difficulty = 1, power = 1, MaxHP = 5, magic = 1, MaxMP = 5, speed = 5, name = "Goblin"): 
    super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name) 

但是,當我試圖讓一個精靈對象沒有完整數量的默認值(比如,我只需要輸入一個難度值),它告訴我我需要完整的8個參數,即使其餘的都是默認值。我有什麼理由不能這樣做,或者我在這裏做錯了什麼?

+3

你錯過了Goblin的'__init__'下劃線。所以你應該複製並粘貼導致問題的代碼。還有完整的追溯。 Python的回溯通常非常有用。 –

回答

3

因爲你叫super(Goblin, self).__init__(self, power, MaxHP, magic, MaxMP, speed, name)沒有difficulty。你可能也想繼承class Enemy(object),以確保Enemy是一個新式的課程,如果你使用的是2.x(我想你一定是這樣,考慮到你已經使用過的舊方法super)。

這裏有一個簡單的例子:

class Animal(object): 
    def __init__(self, talk): 
    print '__init__ Animal: ', talk 

class Cat(Animal): 
    def __init__(self, talk='meow'): 
    print '__init__ Cat' 
    super(Cat, self).__init__(talk) 

if __name__ == '__main__': 
    tom = Cat() 

輸出:

__init__ Cat 
__init__ Animal: meow 

編輯:

那麼如果下面不工作,也許你有老班定義緩存在你的解釋器中(嘗試在新的互聯網上運行它)譯碼)。

class Enemy(object): 
    def __init__(self, difficulty, power, MaxHP, magic, MaxMP, speed, name): 
    self.power = power + 2*difficulty 
    self.HP = self.MaxHP = MaxHP + 5*difficulty 
    self.magic = magic + 2* difficulty 
    self.MP = self.MaxMP = MaxMP + 5*difficulty 
    print 'Raaarghh!! I am the formidable {}.'.format(name) 

class Goblin(Enemy): 
    def __init__(self, difficulty=1, power=1, MaxHP=5, magic=1, MaxMP=5, speed=5, name="Goblin"): 
    super(Goblin, self).__init__(difficulty, power, MaxHP, magic, MaxMP, speed, name) 

if __name__ == '__main__': 
    g = Goblin(name='user1038783 goblin') 
+0

哈,當你想到你嘗試了一切時,你想念的東西。並感謝您的對象部分。 – limasxgoesto0

+0

我試着再次運行程序(而不是我的測試驅動程序),即使有困難,我仍然得到相同的錯誤「TypeError:__init__只需8個參數,2給出」 – limasxgoesto0

+0

好吧,檢查'__init__中的雙下劃線',並且不要明確傳入'self',因爲它隱式完成。 – wim

0

此代碼的工作對我來說:

class Enemy(object): 
    def __init__(self, difficulty, power, MaxHP, magic, MaxMP, speed, name): 
     self.power = power + 2*difficulty 
     self.HP = self.MaxHP = MaxHP + 5*difficulty 
     self.magic = magic + 2* difficulty 
     self.MP = self.MaxMP = MaxMP + 5*difficulty 

class Goblin(Enemy): 
    def __init__(self, difficulty = 1, power = 1, MaxHP = 5, magic = 1, MaxMP = 5, speed = 5, name = "Goblin"): 
     super(Goblin, self).__init__(difficulty, power, MaxHP, magic, MaxMP, speed, name) 

的方法,我不得不改變你得到它的工作:

  1. 修復在Goblin拼錯。
    • 症狀:Goblin()凸起TypeError: __init__() takes exactly 8 arguments (1 given),如精沒有限定的__init__的方法,所以它已被從object
        繼承繼承 Enemy
    • 變化Enemy沒有缺省的一個到一個新的風格類
    • 症狀:我致電super()致電TypeError: must be type, not classobj;我不確定老版本的Python是否會允許它或引發不同的錯誤,但我知道舊樣式的類與新樣式類有不同的MRO(方法解析順序)規則,並且我相信這可能會使得super扭曲無論如何。
  2. 取下呼叫第二selfsuper(Goblin, self).__init__(self, ...)
    • 症狀:self自動傳遞到super(Class, self).some_method(...),因此將其放入有自己就像調用Enemy.__init__(self, self, difficulty, power, ...)
  3. 新增difficulty號召,super(Goblin, self).__init__(...)
    • 症狀:你得到的Goblin.__init__拖欠難度,但後來沒有通過價值高達Enemy.__init__

我認爲就是這樣。

+0

啊,是的,wim提到__init__錯誤輸入,但是當我檢查它時我又錯過了。感謝你們兩個,現在正在工作。 – limasxgoesto0