0

我正在尝试通过制作选课程序来练习 OOP

    # let's make a character selection program

    class player:
      def __init__(self, username, age, weight, height, gender):
        self.username = username
        self.age = age
        self.weight = weight
        self.height = height
        self.gender = gender

    class soldier(player):
      strength = weight*height*2
      print strength

    print "Please enter the following"

    player_username = raw_input("Please enter a username: ")
    player_age = input("Please enter your age: ")
    player_weight = input("Please enter your weight: ")
    player_height = input("Please enter your height: ")
    player_gender  = raw_input("Please enter your gender: ")
    player_character_class = raw_input("Please enter a player class: ")

    character_obj = player(player_username, player_age, player_weight, player_height, player_gender)
    print soldier.strength

但是,我得到了错误

    Traceback (most recent call last):
      File "character_select.py", line 11, in <module>
        class soldier(player):
      File "character_select.py", line 12, in soldier
        strength = weight*height*2
    NameError: name 'weight' is not defined

不太确定如何定义重量。我以为我通过将“玩家”传递给“焊料”来继承它。有人可以帮我吗?

谢谢!

4

3 回答 3

2

由于您将属性分配给playerin ,因此在实例化__init__()之前不会创建它们。player但是,在您的soldier类中,您尝试在类创建时基于当时不存在的变量设置属性,因为它们仅存在于实例(不同类的实例中,不少于)。

我认为您可能想要做的是__init__()soldier. (我还冒昧地将每个 PEP 8 的类名大写。这有助于跟踪哪些名称引用类,即用于构造对象的模板,以及哪些类的实例。)

class Soldier(Player):
    def __init__(self, username, age, weight, height, gender):
        # call parent class to set up the standard player attributes
        Player.__init__(self, username, age, weight, height, gender)
        # now also define a soldier-specific attribute
        self.strength = weight*height*2

然后实例化Soldier而不是Player类,因为你想要一个士兵:

character_obj = Soldier(player_username, player_age, player_weight, player_height, player_gender)
print character_obj.strength

我应该进一步指出:

class Soldier(Player):

不是函数调用。你没有传递 PlayerSoldier. 相反,您说的Soldier一种 Player. 因此,它具有 a 的所有属性和功能Player(您无需再次指定,这就是继承的全部意义)以及您在Soldier. 但是,在声明时您不能直接访问Player(或Player实例)的属性Soldier(不是您通常需要它们)。

于 2013-08-08T20:51:09.993 回答
1

Soldier 是一个类,但您还没有在任何地方实例化它。您已经尝试使用 character_obj 实例化一个玩家,但是当您尝试打印士兵.xxx 时,它正在查看类,而不是任何对象。

于 2013-08-08T20:49:41.230 回答
1

我冒昧地纠正了代码中的一些错误/误解 - 看看这是否有帮助:

# let's make a character selection program

class Player:
    def __init__(self, username, age, weight, height, gender):
        self.username = username
        self.age = age
        self.weight = weight
        self.height = height
        self.gender = gender

class Soldier(Player):
    def __init__(self, *args):
#       First of all, in order to actually inherit the attributes of "player", you need to invoke the __init__ function for "player":
        Player.__init__(self, *args)  # The *args business is to send any arguments that can be handled by the generic player constructor to that player constructor.
#       You probably want to work on the instance variable "strength", so use self.strength, self.weight, and self.height
        self.strength = self.weight*self.height*2
#        print strength

print "Please enter the following"

player_username = raw_input("Please enter a username: ")
player_age = input("Please enter your age: ")
player_weight = input("Please enter your weight: ")
player_height = input("Please enter your height: ")
player_gender  = raw_input("Please enter your gender: ")
player_character_class = raw_input("Please enter a player class: ")

# I'm guessing you actually wanted to make the player a "soldier", not a generic "player"
character_obj = Soldier(player_username, player_age, player_weight, player_height, player_gender)
print character_obj.strength

这是输出:

Please enter the following
Please enter a username: Brionius
Please enter your age: 92
Please enter your weight: 50
Please enter your height: 7
Please enter your gender: yes
Please enter a player class: super soldier
700
于 2013-08-08T20:58:51.430 回答