TypeError:必须使用Pokemon实例作为第一个参数调用unbound方法fight()(获取类型实例)

时间:2016-04-14 17:57:34

标签: python

这是我的代码:

# pokemon1
class Pokemon(object):
    def __init__(self,name,hp,damage):
        self.name = name     
        self.hp = hp        
        self.damage = damage 

    def fight(self,other):
        if(self.hp > 0):
            print("%s did %d damage to %s"%(self.name,self.damage,other.name))
            other.hp -= self.damage
            if (other.hp > 0):
                print("%s has %d hp left" % (other.name, other.hp))
            else:
                print("%s has died" % (other.name))
            return other.fight(self)
        else:
            print("%s wins! (%d hp left)"%(other.name,other.hp))
            return other,self  

class pikachu(Pokemon):
    def __init__(self):
        super(pikachu, self).__init__('pikachu', 100, 10)

class pidgy(Pokemon):
    def __init__(self):
        super(pidgy, self).__init__('pidgy', 200, 12)

#main
import pokemon1

pikachu = pokemon1.pikachu
pidgy = pokemon1.pidgy

p = pokemon1.Pokemon
p.fight(pikachu,pidgy)

2 个答案:

答案 0 :(得分:0)

你需要在你的倒数第二行调用Pokemon(),而不是Pokemon来调用构造函数。

答案 1 :(得分:0)

Reinderien是对的,但我尝试了你的代码并发现了一个不同的问题,你设计的类与你尝试使用它的方式不同。

此代码应该起作用:

pikachu = pokemon1.pikachu
pidgy = pokemon1.pidgy

pikachu.fight(pidgy)