将self作为类中的方法参数传递 - Python

时间:2018-04-06 19:22:51

标签: python python-3.x class parameters parameter-passing

我正在使用Python 3,我想用下面的Card类模拟纸牌游戏:

class Card:
    def __init__(self, name = "", stars = 0, attack = 0):
        self.name = name
        self.stars = stars
        self.attack = attack

    def setName(self, name):
        self.name = name

    def getName(self):
        return self.name

    def setStars(self, stars):
        self.stars = stars

    def getStars(self):
        return self.stars

    def setAttack(self, attack):
        self.attack = attack

    def getAttack(self):
        return self.attack

    def attack(self, enemyCard):
        print(self.name + "attacked" + enemyCard.getName() + "and did" + self.attack + "damage!")

以下是我的驱动程序类:

def main():
    me = Card()
    me.setName("Good")
    me.setStars(99)
    me.setAttack(9000)

    you = Card()
    you.setName("EVIL")
    you.setStars(0)
    you.setAttack(-1)

    me.attack(you)      <-error here

除了最后一行 me.attack(你)之外,一切都有效:

TypeError: 'int' object is not callable

我认为它可能与中的攻击方法有关,但我不太明白为什么变量被视为一个int

0 个答案:

没有答案