2015-12-10 33 views
0

我是初學者,偶然遇到了繼承問題。繼承問題我不明白

當我使用的代碼塊,程序不正確地感謝在進入功能工作線:

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'時,當我使用'超級'來繼承輸入功能時,程序結束。如果我不這樣做,程序工作:

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

我知道我必須失去了一些東西明顯。

謝謝,戴夫。

回答

2

忘記返回調用super().enter()的結果。您正在吞嚥返回值,所以您從未返回新房間。下面是正確的:

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

這並不是說在定義新Bathroom().enter()方法,如果你要做的就是調用原始版本super()點。您也可以刪除整個enter()方法(正如我爲上面的__init__方法所做的那樣)。

+0

欣賞它。謝謝Martijn! –