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

时间:2020-12-29 23:40:45

标签: python

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

这里是错误

第 46 行,在

print("Brains:", hero.brains)

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

#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("")

2 个答案:

答案 0 :(得分:1)

你需要告诉 SuperHero() 英雄的属性是什么。您在类外定义它们只会创建与类具有 0 关系的变量。我建议这样做:

hero = SuperHero(superName = input("Please enter your Super hero name:"),brains = random.randint(1,20)

然后您可以打印分配给您刚刚创建的对象“英雄”的属性:

print("Your name is {}".format(hero.superName))

答案 1 :(得分:1)

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

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

A) 你在构造函数中将它们作为参数传递,所以

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) 由于您的统计数据都是随机的,您可以告诉构造函数将它们分配给随机数,这将您的代码缩短了几行,但不会让您在创建对象时分配自己的统计数据,如下所示:

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)