继承问题我不明白

时间:2015-12-10 12:29:42

标签: python inheritance

我是初学者,偶然发现了继承问题。

当我使用这段代码时,由于enter函数中的一行,程序无法正常工作:

class Bathroom(Room):

    def __init__(self):
        super(Bathroom, self).__init__("bathroom")


    def enter(self, world, player):
        super(Bathroom, self).enter(world, player)

但是,当我使用它时,它确实:

class Bathroom(Room):

    def __init__(self):
        super(Bathroom, self).__init__("bathroom")

他们不是一回事吗?

我写的完整脚本(未完成顺便说一句)如下。当我在被问到“你想离开”后输入'y'时,程序在我使用'super'继承enter函数时结束。如果我不这样做,该程序将起作用:

while player and self.name != "corridor":

            response = self._ask_question("Do you want to leave? (y/n) ", "y", "n")

            if response == "y":
                return world.corridor
            elif response == "n" and self.enemy:
                print("The", self.enemy, "kills you. You didn't even put up a\
fight")
                return world.death

完整脚本:

import random
import time
# bad from sys import exit

class Character(object):

    def __init__(self, name, health, attack):
        self.name = name
        self.health = health
        self.attack = attack

    def __str__(self):
        return str(self.health) + " health and " + str(self.attack) + " attack."



class Room(object):

    def __init__(self, name):
        self.name = name
        self.enemy = self._getRandEnemy()


    def enter(self, world, player):
        print(player.name + ",", "you are in the", self.name + ". You have\
 " + str(player))

        if self.enemy: # you may have killed it
            print("But, wait! There's a", self.enemy.name, "with " + str(\
                self.enemy))
            response = self._ask_question("Do you stay and fight?(y/n)\
", "n", "y")
            if response == "n":
                pass

            if response == "y":
                self.combat(player, self.enemy)

        else:
            print("No enemies here.")


        # check if player has no health after potential fight
        if player.health < 1:
            return world.death

        while player and self.name != "corridor":

            response = self._ask_question("Do you want to leave? (y/n) ", "y", "n")

            if response == "y":
                return world.corridor
            elif response == "n" and self.enemy:
                print("The", self.enemy, "kills you. You didn't even put up a\
fight")
                return world.death



    def _getRandEnemy(self):

        names = ["Troll", "Witch", "Ogre", "Jeremy Corbyn"]

        return Character(random.choice(names), random.randint(4, 6),\
                         random.randint(2, 3))


    def _ask_question(self, question, a, b):

        response = None
        while response not in(a, b):
            response = input(question)

        return response


    def combat(self, player, enemy):
        while player.health > 0 and enemy.health > 0:
            time.sleep(1)
            print("You attack and deal", player.attack, "damage")
            enemy.health -= player.attack
            if enemy.health >= 1:
                print("The enemy has " + str(self.enemy))
                time.sleep(1)
                print("The", self.enemy.name, "attacks and deals you",\
                      self.enemy.attack, "\
damage.")
                player.health -= enemy.attack
                print("You have " + str(player))



        if player.health < 1:
            pass
        if enemy.health < 1:
            print("Ha! Got him!")
            self.enemy = None





class Corridor(Room):

    def __init__(self):
        self.name = "corridor"
        self.enemy = None


    def enter(self, world, player):

        super(Corridor, self).enter(world, player)
        room = self._ask_question("Which room: bathroom, bedroom ",\
                                      "bedroom", "bathroom", "Library", "study")

        if room == "bedroom":
            return world.bedroom

        if room == "bathroom":
            return world.bathroom




class Bathroom(Room):

    def __init__(self):
        super(Bathroom, self).__init__("bathroom")


    def enter(self, world, player):
        super(Bathroom, self).enter(world, player)



class Bedroom(Room):

    def __init__(self):
        super(Bedroom, self).__init__("bedroom")



class Death(Room):

    def __init__(self):
        super(Death, self).__init__("death")


    def enter(self, world, player):
        time.sleep(1)
        responses = ["Off to the man in sky. You are dead", "You died,\
 no-one cried.", "Lolz. You're dead!"]
        print(random.choice(responses))

        return None



class World(object):

    def __init__(self):

        self.corridor = Corridor()
        self.bathroom = Bathroom()
        self.death = Death()
        self.bedroom = Bedroom()
        self.start = self.corridor



def play_game(world, player):

    room = world.start

    while room:

        room = room.enter(world, player)

play_game(World(), Character("Bob", 12, 2))

我知道我必须遗漏一些明显的东西。

谢谢,戴夫。

1 个答案:

答案 0 :(得分:2)

忘记返回 super().enter()来电的结果。您正在吞咽返回值,因此您永远不会返回新房间。以下是正确的:

class Bathroom(Room):
    def enter(self, world, player):
        return super(Bathroom, self).enter(world, player)

如果您所做的只是使用Bathroom().enter()调用原始版本,那么定义新的super()方法并不重要。您也可以删除整个enter()方法(正如我上面针对__init__方法所做的那样)。