1

ファイル 1:

class Rogue():
    def __init__(self):
        self.name = "Rogue"
        Hero.__init__(self.name, None)

'''class Barbarian(Hero):
    Hero.__init__(self, name, bonuses)

class Mage(Hero):
    Hero.__init__(self, "Mage", bonuses)'''


class Hero(Tile):
'''A class representing the hero venturing into the dungeon.
Heroes have the following attributes: a name, a list of items,
hit points, strength, gold, and a viewing radius. Heroes
inherit the visible boolean from Tile.'''

def __init__(self, name, bonuses=(0, 0, 0)):
    '''(Hero, str, list) -> NoneType
    Create a new hero with name name,
    an empty list of items and bonuses to
    hp, strength, gold and radius as specified
    in bonuses'''

    self.name = name
    self.items = []
    #Rogue
    if self.name == "Rogue":
        self.hp = 10 + bonuses[0]
        self.strength = 2 + bonuses[1]
        self.radius = 2 + bonuses[2]
    #Barbarian
    elif self.name == "Barbarian":
        self.hp = 12 + bonuses[0]
        self.strength = 3 + bonuses[1]
        self.radius = 1 + bonuses[2]
    #Mage
    elif self.name == "Mage":
        self.hp = 8 + bonuses[0]
        self.strength = 2 + bonuses[1]
        self.radius = 3 + bonuses[2]

    Tile.__init__(self, True)

ファイル 2:

class GameScreen:
    '''Display the current state of a game in a text-based format.
    This class is fully implemented and needs no
    additional work from students.'''

def initialize_game(self):
    '''(GameScreen) -> NoneType
    Initialize new game with new user-selected hero class
    and starting room files.'''

    hero = None
    while hero is None:
        c = input("Select hero type:\n(R)ogue (M)age (B)arbarian\n")
        c = c.lower()
        if c == 'r':
            hero = Rogue()
        elif c == 'm':
            hero = Mage()
        elif c == 'b':
            hero = Barbarian()

    self.game = Game("rooms/startroom", hero)

複数の異なるファイルがありますが、重要なのはこれらだけです。上記のコードは、入力を要求し、入力に基づいてヒーロー クラスを呼び出します。クラスは私が作成しなければならない部分です。特定のパラメータで Hero を呼び出すクラス Rogue を作成しました。次のエラーが表示されます。

File "/Users//Documents/CSC148/Assignment 2/hero.py", line 7, in __init__
Hero.__init__(self.name, None)
  File "/Users//Documents/CSC148/Assignment 2/hero.py", line 30, in __init__
self.name = name
builtins.AttributeError: 'str' object has no attribute 'name'

文字列を変更するのではなく、文字列があるかどうかを確認するだけです。単純な「self.name」コンストラクターの属性名が文字列にないことを教えてくれるのはなぜですか?

4

2 に答える 2

2

あなたがするとき何が起こっていますか

Hero.__init__(self.name, None)

「self」パラメーターが最初の引数として暗黙的に渡されないことです。したがって、この場合、実際には最初の引数 (self の代わりに) として文字列 (self.name) を渡し、'name' パラメーターの代わりに None を渡します。「bonuses」がキーワード パラメータでない場合、この呼び出しはTypeError: __init__() takes exactly 3 arguments (2 given)

そのため: self.name は self を表し、 None は name を表し、ボーナスはデフォルト (0, 0, 0) に初期化されます

于 2012-10-17T21:15:44.037 に答える
0

Hero.__init__オブジェクトを初期化しHeroます。新しい Hero オブジェクトを作成するには、 を呼び出す必要がありますHero。したがって、Rogue.__init__では、行

Hero.__init__(self.name, None)

が故障しています。新しいヒーロー オブジェクトを作成するには、次のいずれかを行います。

class Rogue:
    def __init__(self):
        self.name = "Rogue"
        self.enemy = Hero(self.name, None)

またはRogueのサブクラスにするHero

class Rogue(Hero):
    def __init__(self):
        super().__init__("Rogue", None)
于 2012-10-17T21:05:58.677 に答える