Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用多态性重写_Python - Fatal编程技术网

Python 使用多态性重写

Python 使用多态性重写,python,Python,覆盖生活的去天堂()方法 当一种动物真的上天堂时,一种食物 肉”和动物的食物价值。获取食物价值()将在该位置创建 动物在哪里。例如,如果一只名为“熊”的动物被杀死,一个食物物体 将生产名为“熊肉”的产品。 要将对象添加到地点,可以使用地点的“添加对象”方法 反对 预定义代码: class LivingThing(MobileObject): def __init__(self, name, health, threshold): super().__init__(name

覆盖生活的
去天堂()
方法 当一种动物真的上天堂时,一种食物 肉”和
动物的食物价值。获取食物价值()
将在该位置创建 动物在哪里。例如,如果一只名为“熊”的动物被杀死,一个
食物
物体
将生产名为“熊肉”的产品。
要将对象添加到地点,可以使用地点的“添加对象”方法
反对

预定义代码:

class LivingThing(MobileObject):
    def __init__(self, name, health, threshold):
        super().__init__(name, None)
        self.health = health
        self.threshold = threshold

    def get_threshold(self):
        return self.threshold

    def get_health(self):
        return self.health

    def add_health(self, health):
        self.health = min(100, self.health+health)

    def reduce_health(self, health):
        self.health = max(0, self.health-health)
        if self.health == 0:
            self.go_to_heaven()

    def go_to_heaven(self):
        self.get_place().del_object(self)
        HEAVEN.add_object(self)
        GAME_LOGGER.add_event("DEAD", self)

class Food(Thing):
    def __init__(self, name, food_value):
        self.name = name
        self.food_value = food_value
    def get_food_value(self):
        return self.food_value
这是我的密码:

class Animal(LivingThing):
    def __init__(self, name, health, food_value, threshold = random.randint(0, 4)):
        super().__init__(name, health, threshold)
        self.food_value = food_value
    def get_food_value(self):
        return self.food_value
    def get_threshold(self):
        return self.threshold
    def go_to_heaven(self):
        self.get_place().add_object(self)
        super().go_to_heaven()
要测试我的代码:

BASE.add_object(bear)
print(named_col(BASE.get_objects()))    # ['bear']
print(bear.get_place().get_name())      # Base
bear.go_to_heaven()                     # bear went to heaven!
print(named_col(BASE.get_objects()))    #['bear meat'] 

最后一部分我没有得到“熊肉”,而是得到了“熊”。我的代码出了什么问题?

因为这似乎是家庭作业,我不想给你一个完整的答案,但请注意

def go_to_heaven(self):
    self.get_place().add_object(self)
    super().go_to_heaven()
你把物体放回原来的位置。相反,你想放些别的东西

def go_to_heaven(self):
    self.get_place().add_object(** some other object here **)
    super().go_to_heaven()
试着替换

def go_to_heaven(self):
        self.get_place().add_object(self)
        super().go_to_heaven()
与:


你想把肉放在死亡的地方,而不是动物的鬼魂。

你被覆盖的
去天堂
做的是
自我。获取位置()。添加对象(自我)
,它将动物添加到自己的位置,而不是添加食物。你知道缩进很重要吗?我尝试添加,但失败了。如何修改我的代码?最简单的事情是:从编辑器中复制代码,粘贴到表单中,标记所有代码,然后按“源代码格式”按钮。@JoranBeasley,为什么不这样做呢。他以有秩序、有礼貌的方式问了这个问题,展示了他所做的尝试,并确定了他的身份。那就是互相帮助,不是吗?这是家庭作业这一事实是不现实的。我给了你另外一个:P@Hyperboreus非常感谢……)但是你能给我解释一下这个代码的作用吗?没什么好解释的。它创建一个新的食物对象(使用提供的
\uuuu init\uuuu
)并具有必要的名称和值,然后将其添加到该位置。
def go_to_heaven(self):
        self.get_place().add_object(Food('{} meat'.format(self.name), self.food_value))
        super().go_to_heaven()