首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用类正确定义对象?

如何使用类正确定义对象?
EN

Stack Overflow用户
提问于 2020-12-30 07:40:46
回答 2查看 43关注 0票数 1

我是Python的新手。在来这里询问之前,我已经搜索了几个小时来了解为什么会发生这种情况。请帮助我理解如何正确使用类/对象。提前感谢您的帮助和耐心。

下面是错误

第46行,在

print("Brains:",hero.brains)

AttributeError:'Superhero‘对象没有'brains’属性

代码语言:javascript
复制
#Importing Random Module

import random

#create Superhero class

class Superhero():
    #initializing our class and setting its atrributes
    def _init_(self):
        self.superName = superName
        self.power = power
        self.braun = braun
        self.brains = brains
        self.stamina = stamina
        self.wisdom = wisdom
        self.constitution = constitution
        self.dexterity = dexterity
        self.speed = speed

# adding random values to each

braun = random.randint(1,20)
brains = random.randint(1,20)
stamina = random.randint(1,20)
wisdom = random.randint(1,20)
constitution = random.randint(1,20)
dexterity = random.randint(1,20)
speed = random.randint(1,20)


print("Please enter your Super Hero name: ")

# creating the super hero object
hero = Superhero()

# assigning a value to superName using the user's input
hero.superName = input('>')

# print the result of the created object, including its parameters

print("Your name is %s. " % (hero.superName))
print("Your new stats are: ")
print("")
print("Brains: ", hero.brains)
print("Braun: ", hero.braun)
print("Stamina: ", hero.stamina)
print("Wisdom: ", hero.wisdom)
print("Constitution: ", hero.constitution)
print("Dexterity: ", hero.dexterity)
print("Speed: ", hero.speed)
print("")
EN

回答 2

Stack Overflow用户

发布于 2020-12-30 07:53:14

您需要告诉SuperHero()英雄的属性是什么。在类外部定义它们只会创建与类没有关系的变量。下面是我的建议:

代码语言:javascript
复制
hero = SuperHero(superName = input("Please enter your Super hero name:"),brains = random.randint(1,20)

然后,您可以打印出分配给刚才创建的对象"hero“的属性:

代码语言:javascript
复制
print("Your name is {}".format(hero.superName))
票数 1
EN

Stack Overflow用户

发布于 2020-12-30 07:55:50

为了确认,这是你的完整代码吗?因为这里的问题很简单。您忘记将参数添加到类构造函数中。您为统计数据分配了随机数,但它们超出了对象范围,这意味着您的超级英雄不会访问这些变量,除非您将它们作为参数传递给您的构造函数。

有两种方法可以解决这个问题:

A)将它们作为参数传递到构造函数中,因此

代码语言:javascript
复制
class Superhero():
    #initializing our class and setting its atrributes
    def _init_(self, superName, power, braun, brains, stamina, wisdom, constitution, dexterity, speed):
        self.superName = superName
        self.power = power
        self.braun = braun
        self.brains = brains
        self.stamina = stamina
        self.wisdom = wisdom
        self.constitution = constitution
        self.dexterity = dexterity
        self.speed = speed

print("Please enter your Super Hero name: ")
superName = input('>')

power = random.randint(1,20)
braun = random.randint(1,20)
brains = random.randint(1,20)
stamina = random.randint(1,20)
wisdom = random.randint(1,20)
constitution = random.randint(1,20)
dexterity = random.randint(1,20)
speed = random.randint(1,20)

hero = Superhero(superName, power, braun, brains, stamina, wisdom, constitution, dexterity, speed)

然后你可以打印任何你想要的东西。

B)因为你的统计数据都是随机的,你可以告诉构造函数将它们分配给随机数,这会将你的代码缩短几行,但不会让你在创建对象时分配自己的统计数据,如下所示:

代码语言:javascript
复制
class Superhero():
    #initializing our class and setting its atrributes
    def _init_(self, superName):
        self.superName = superName
        self.power = random.randint(1,20)
        self.braun = random.randint(1,20)
        self.brains = random.randint(1,20)
        self.stamina = random.randint(1,20)
        self.wisdom = random.randint(1,20)
        self.constitution = random.randint(1,20)
        self.dexterity = random.randint(1,20)
        self.speed = random.randint(1,20)

print("Please enter your Super Hero name: ")
superName = input('>')

hero = Superhero(superName)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65500184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档