错误:未绑定方法Dragon()必须以Enemy实例作为第一个参数来调用(取而代之的是Player实例) [英] Error: unbound method Dragon( ) must be called with Enemy instance as first argument (got Player instance instead)

查看:80
本文介绍了错误:未绑定方法Dragon()必须以Enemy实例作为第一个参数来调用(取而代之的是Player实例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Character: 
    def __init__(self):
        self.name = ""
        self.health = 1
        self.health_max = 1

class Player(Character):
    def __init__(self):
        Character.__init__(self)
        self.state = 'normal'
        self.health = 10
        self.health_max = 10

class Monster(Character):
    def Dragon(self):
        self.name = "Dragon"
        self.health = 20

    def Goblin(self):
        name = "Goblin"
        health = 5

p = Player()
p.name = raw_input("Please enter your name: ")
print p.name
print p.state
print p.health
print p.health_max

m = Monster()
enemy = m.Dragon
print enemy.name
print enemy.health

对不起,我已经简化了一些解释我遇到的困难.我在OOP的基础方面遇到了一些麻烦,并且在这段代码中遇到了问题.我正在尝试在此处创建龙",但遇到以下错误:

Sorry, I've made this a little simpler to explain what I'm having difficulty with. I'm having a little bit of trouble with the basics of OOP, and I'm running into an issue with this snippet of code. I'm trying to create a "Dragon" here but I'm running into the following error:

回溯(最近通话最近): 在第32行的文件"test2.py"中 打印敌人 AttributeError:函数"对象没有属性名称"

Traceback (most recent call last): File "test2.py", line 32, in print enemy.name AttributeError: 'function' object has no attribute 'name'

您能告诉我我在做什么错吗?谢谢.

Can you tell me what I'm doing wrong here? Thanks.

推荐答案

您必须先创建一个类的实例,然后才能从该类中调用任何函数:

You have to create an instance of a class first before you call any functions from it:

myenemy = Enemy()
myenemy.Dragon()

在您的代码中,看起来就像您创建了self.enemy,但是稍后您调用了self.enemy = Enemy.Dragon(self).代替最后一行,放置self.enemy = self.enemy.Dragon(self).

In your code, it looks like you created self.enemy, but later you call self.enemy = Enemy.Dragon(self). Instead of this last line, put self.enemy = self.enemy.Dragon(self).

在您的其余代码中,这似乎也是一个反复出现的问题. Commands = {'explore': Player.explore}应该是Commands = {'explore': p.explore}(在创建实例p之后).

It seems to be a recurring issue in the rest of your code as well. Commands = {'explore': Player.explore} should probably be Commands = {'explore': p.explore} (after you have created the instance p).

自从您更新了代码以来,我认为您正在混淆函数和类. Dragon是一个功能,执行enemy = m.Dragon时,您只是将功能复制到敌人身上.因此,当您enemy.name认为它是一个类时,会引发错误,因为enemy现在是一个函数,而不是实例.

Since your updated code, I think you're getting functions and classes mixed up. Dragon is a function, and when you do enemy = m.Dragon, you are simply copying the function onto enemy. And thus when you do enemy.name, thinking it's a class, an error is raised, because enemy is now a function, not an instance.

您将必须为不同的怪物创建单独的类:

You will have to create separate classes for different monsters:

class Dragon:
    self.name = "Dragon"
    self.health = 20

class Goblin:
    name = "Goblin"
    health = 5

这篇关于错误:未绑定方法Dragon()必须以Enemy实例作为第一个参数来调用(取而代之的是Player实例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆