3

简单的问题,对你们中的一个人来说可能非常明显,但我不确定为什么会发生这种情况。所以这里是我制作的三个 python 文件。

主要字符类:

class Character():
    """
    This is the main parents class for creation of
    characters, be they player, NPC or monsters they
    shall all share common traits
    """

    def __init__(self, name, health, defense):
        """Constructor for Character"""
        self.name = name
        self.health = health
        self.defense = defense

球员等级:

from character import *

class Player(Character):
    """
    The player class is where heros are made
    They inherit common traits from the Character class
    """

    def __init__(self, name, health, defense, str, int):
        Character.__init__(self, name, health, defense)
        self.str = str
        self.int = int

在里面:

from Letsago.player import Player


hero = Player("Billy", 200, 10, 10, 2)    
print hero.name

这导致:

Billy
Billy

为什么会被退回两次?

4

1 回答 1

7

我已将您的示例放在一个名为test.py

class Character():
    """
    This is the main parents class for creation of
    characters, be they player, NPC or monsters they
    shall all share common traits
    """

    def __init__(self, name, health, defense):
        """Constructor for Character"""
        self.name = name
        self.health = health
        self.defense = defense


class Player(Character):
    """
    The player class is where heros are made
    They inherit common traits from the Character class
    """

    def __init__(self, name, health, defense, str, int):
        Character.__init__(self, name, health, defense)
        self.str = str
        self.int = int


hero = Player("Billy", 200, 10, 10, 2)
print hero.name

并执行以下(ubuntu 13.04 上的 python 2.7):

python test.py

并在控制台中得到以下信息

Billy

尝试像我在一个文件中那样隔离示例并执行它(在交互式外壳之外)。还要检查您的模块并检查您的from character import *. 确保您正在导入正确的Player

于 2013-06-10T02:23:25.250 回答