在python中遇到继承问题

时间:2016-12-08 03:28:21

标签: python inheritance

我遇到类之间的继承问题。我正在努力的类是vulture类,它是bird类的子类,它是critter类的子类。鸟类和小动物课程,以及它们正在经历的主要课程,一切正常。但是当我尝试使用vulture类时,我收到一个错误:

AttributeError: 'Vulture' object has no attribute '_Bird__direction'

我不确定我做错了什么,我希望有人可以帮助我找出错误。

秃鹰类:

from bird import *
class Vulture (Bird):
    def __init__(self):
        self.__hungry=True
        self.__in_a_row=-1
        self.__direction=DIRECTION_NORTH

    def eat(self):
        if self.__hungry:
            self.__hungry=False
            return True
        else:
            return False

    def fight(self, opponent):
        self.__hungry=True
        return super(Bird, self).fight(self,opponent)
    def get_color(self):
        black

鸟类:

from Critter import *

class Bird (Critter):
    def __init__(self):
        self.__in_a_row=-1 #doesn't go north 3 times on first move if set to 0
        self.__direction=DIRECTION_NORTH

    def fight(self, opponent):
        if opponent.__str__=="%":  #roars at ants
            return ATTACK_ROAR
        else:
            return ATTACK_POUNCE

    def get_color(self):
        return "blue" 

    def __str__(self): #uses the most recent direction to determine which character should be used
        if (self.__direction==DIRECTION_NORTH or self.__direction==DIRECTION_CENTER):
            return "^"
        elif self.__direction==DIRECTION_EAST:
            return ">"
        elif self.__direction==DIRECTION_SOUTH:
            return "V"
        else:
            return "<"

    def get_move(self):
        if self.__in_a_row<2:
            self.__in_a_row+=1
            return self.__direction
        else:   #Turning in the proper direction after it hits the end of the line
            if self.__direction==DIRECTION_NORTH:
                self.__direction=DIRECTION_EAST
                self.__in_a_row=0
                return self.__direction
            elif self.__direction==DIRECTION_EAST:
                self.__direction=DIRECTION_SOUTH
                self.__in_a_row=0
                return self.__direction
            elif self.__direction==DIRECTION_SOUTH:
                self.__direction=DIRECTION_WEST
                self.__in_a_row=0
                return self.__direction
            elif self.__direction==DIRECTION_WEST:
                self.__direction=DIRECTION_NORTH
                self.__in_a_row=0
                return self.__direction

    def eat(self):
        return False

我不认为生物类本身会与此相关,并且它非常大,所以除非有人认为它可能会有所帮助,否则我会将其保留在帖子之外。

1 个答案:

答案 0 :(得分:0)

请参阅Python文档中的Private Variables部分。

Python专门处理以双下划线开头的实例变量,修改名称以使它们对类保持私有(甚至是父类的私有)。当您在Vulture类上调用Bird方法时,对self.__direction的Bird方法引用实际引用self._Bird__direction变量,该变量与self._Vulture__direction变量不同您在Vulture __init__构造函数中初始化。

特别是:

v = Vulture()   # initializes self._Vulture__direction only
print(v)        # throws an exception -- v._Bird__direction isn't initialized
编辑:正如@ShadowRanger所指出的那样,Python中的约定是使用单个下划线作为实例变量或方法的前缀,这些变量或方法旨在对实现是私有的,但在超类和子类之间自由共享(即类似于{{ Java或C ++中的1}}。语言没有对这些变量进行特殊处理,它只是向类用户发出的一个信号,即他们不应该“混淆”这些实例变量或方法。

这可能是您对protected__direction甚至__in_a_row的预期(尽管最后一个没有出现在Bird类中)。如果您将所有这些双下划线前缀更改为单个下划线,则应该可以解决您的问题。