2013-07-01 27 views
3

我不明白腳本如何獲得下一個房間,以及「引擎」和「地圖」類的工作方式。這裏是摘錄:Learn Python the Hard Way練習43

Class Map(object): 

    scenes = { 
     'central_corridor': CentralCorridor(), 
     'laser_weapon_armory': LaserWeaponArmory(), 
     'the_bridge': TheBridge(), 
     'escape_pod': EscapePod(), 
     'death': Death() 
    } 

    def __init__(self, start_scene): 
     self.start_scene = start_scene 

    def next_scene(self, scene_name): 
     return Map.scenes.get(scene_name) 

    def opening_scene(self): 
     return self.next_scene(self.start_scene) 

class Engine(object): 

    def __init__(self, scene_map): 
     self.scene_map = scene_map 

    def play(self): 
     current_scene = self.scene_map.opening_scene() 

     while True: 
      print "\n--------" 
      next_scene_name = current_scene.enter() 
      current_scene = self.scene_map.next_scene(next_scene_name) 

我簡直不明白這些部分是如何工作的。我知道類和對象實例和屬性以及所有其他OOP東西是如何工作的,但由於某些原因,我沒有得到這部分代碼。主要是Map類。如果有人能解釋它,那將是非常棒的。

另外(這可能需要閱讀練習),爲什麼需要這兩個類?難道你不能只用類方法來完成它(即沒有自身作爲參數的方法)?然後你可以調用,例如CentralCorridor.enter()。事實上,我在閱讀答案之前就已經解決了這個問題,結果很好。

對不起,我的主要問題是Engine和Map類是如何工作的。另一件事是次要的。

在此先感謝!

+3

沒有閱讀練習,我會說這些類肯定不是*必需的*來解決問題*一般*。這是有人做出的設計決定。你可能選擇了不同的設計,甚至可能選擇了一個完全不涉及OOP的設計。設計可以是好還是壞,好或壞。在設計解決方案時,您可以遵循一些原則,例如[*關注分離*](http://en.wikipedia.org/wiki/Separation_of_concerns)。 –

+1

我相信這是一個糟糕的類使用的例子。 'Map'可以被其'scenes'屬性取代。另外很顯然'Engine'類不應該是一個類。任何只有'__init__' +一個方法的類實際上是一種編寫函數的複雜方式。 – Bakuriu

+0

我還沒有看過這個練習來自哪個網站,但是使用一個類可能會使它更合適。畢竟,這就是所謂的「Learn Python the Hard Way」:) –

回答

7

Map對象是映射場景的類。它有一些場景保存在一個數組中。

scenes = { 
    'central_corridor': CentralCorridor(), 
    'laser_weapon_armory': LaserWeaponArmory(), 
    'the_bridge': TheBridge(), 
    'escape_pod': EscapePod(), 
    'death': Death() 
} 

在地圖的目標是讓你也給它的開幕式現場作爲構造

def __init__(self, start_scene): 
    self.start_scene = start_scene 

看到這將創建一個名爲start_scene包含您的開盤現場在Map的變量。

此外Map有2種方法

# This one returns a scene based on its name or key in the scenes array 
def next_scene(self, scene_name): 
    return Map.scenes.get(scene_name) 


# And this one returns the opening scene which is set when you create the map. 
def opening_scene(self): 
    return self.next_scene(self.start_scene) 

Engine似乎控制場景時玩什麼玩。

# When creating an Engine object you give the map containing scenes to its constructor 
def __init__(self, scene_map): 
     self.scene_map = scene_map 

# The method which starts playing the scenes 
def play(self): 

    # the opening scene from the map is selected as the current scene 
    current_scene = self.scene_map.opening_scene() 

    # You loop all the scenes probably, conditions of this loop are unknown because you haven't posted it entirely. 
    while True: 
     print "\n--------" 
     # It seems the next scene name is known in the current scene 
     next_scene_name = current_scene.enter() 

     # It replaces current scene with the next scene from the map 
     current_scene = self.scene_map.next_scene(next_scene_name) 

爲什麼反正有這兩類需要它?

實在不行,除非它是根據你的任務

像你說的這是可能做到這一點沒有,但有充分的理由要求這樣做。

這樣你就可以用自己的責任制作兩個獨立的課程。 當應用程序變得越來越大時,這種方式的代碼更具可讀性。在應用程序中導航很容易。你可以很容易地更改你的應用程序等部分等。我的建議是保持練習和閱讀面向對象,你會注意到爲什麼你做你看到的東西。

+1

'它有一些靜態場景''靜態'這個詞是OOP中的一個藝術術語,這意味着它具有某些內涵,不適用於你正在談論的內容,因此它可能是混淆和非感性的。 – 7stud

+0

錯誤地使用術語,英語不是我的母語,我不用靜態詞就掙扎着說什麼。修正可能嗎?或「以前設置」?我的英語幾乎包含了我工作中的術語。今天我打電話給書目索引一個'ListView' * facepalm – Timmetje

+0

Ohhhhhhhhh .....它只是點擊。謝謝!我無法圍繞它思考。我認爲這就是點符號上的點符號。 – Aristides

6

Map類有一個關鍵字場景名稱的字典,以及不同類的實例作爲值。要檢索其中一個實例,您需要調用一個傳入作爲場景名稱的字符串的方法。 Map類然後返回相應類的一個實例。

難道你不能用類方法來代替它嗎(即方法 沒有自我作爲參數)?然後,您可以撥打電話,例如 CentralCorridor.enter()。

是的,你可以。缺點是現在你已經在你的代碼中硬編碼了場景類的名字。如果您稍後決定重寫程序以刪除某些場景或添加其他場景,則必須更改代碼中硬編碼名稱的所有位置。如果您使用Map類,則只需對字典進行更改。儘管如此,仍然必須消除嘗試檢索已刪除的類實例的方法調用。或者,您可以使用next_scene()方法處理嘗試檢索已從字典中移除的場景。

類方法的另一個考慮是:你需要存儲'狀態'嗎?比如,現場有多少玩家。而且,您是否需要創建該類型的多個場景?

+0

這個答案對問題的第二部分很有幫助,謝謝! – Aristides

相關問題