使用继承传递变量

时间:2013-08-08 20:42:57

标签: python python-2.7

我试图通过制作课程选择课程来练习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

不确定如何定义重量。我以为我通过传递&#34;玩家&#34;继承了它。进入&#34;焊接&#34;。有人可以帮我吗?

谢谢!

3 个答案:

答案 0 :(得分:2)

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

我认为您可能想要做的是为__init__()编写soldier方法。 (我也可以自由地将每个PEP 8的类名称大写。这有助于跟踪哪些名称引用类,即 templates 用于构造对象,以及哪些类的实例。)< / p>

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):

不是函数调用。您并非 Player传递给Soldier。相反,您说Soldier Player。因此,它具有Player的所有属性和功能(您不需要再次指定,这是整个继承点)以及您在Soldier中定义的任何其他属性和功能。但是,在声明Player时,您无法直接访问Player(或Soldier实例)的属性(而不是您通常需要它们)。

答案 1 :(得分:1)

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

答案 2 :(得分: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