困惑关于类在学习Python的硬的方式ex43? [英] Confused about classes in Learn Python the Hard Way ex43?
问题描述
我很困惑地图和引擎类如何一起运行这个Adventureland类型游戏(完整的代码在这里: http://learnpythonthehardway.org/book/ex43.html )。我想我明白地图类发生了什么,但我真的很困惑的是什么发生在Engine()和为什么scene_map变量是需要的。
class Map(object):
scenes = {
'central_corridor':CentralCorridor(),
'laser_weapon_armory':LaserWeaponArmory b $ b'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)
$ b b def open_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\\\
--------
next_scene_name = current_scene.enter()
current_scene = self.scene_map.next_scene(next_scene_name)
a_map =地图('central_corridor')
a_game =引擎(a_map)
a_game.play()
$ b
引擎
实例的 scene_map
是 Map
类的实例,就像全局 a_map
是。实际上, a_game.scene_map
是与 a_map
相同的实例
所以,无论你可以在顶层使用 a_map
, Engine.play
代码可以使用 self.scene_map
。可能值得把所有东西输入到交互式解释器中直到 a_map
定义,并使用a_map进行操作,以确保你知道它能为你做什么。
那么,为什么 Engine
需要 self.scene_map
?为什么不能使用全局 a_map
?
。问题是,如果你这样做,你将不能创建两个 Engine
实例,没有他们争夺相同的 a_map
。 (这是同样的原因,你不想在函数中使用全局变量。对象不添加一个新的问题 - 事实上,对象的大部分是解决 global-变量问题。)
I'm confused on how the Map and Engine classes together work together to run this Adventureland type game (full code here: http://learnpythonthehardway.org/book/ex43.html). I think I understand what is happening in the Map class, but I'm really confused about what is happening in Engine() and why the scene_map variable is needed.
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)
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()
Thank you for any help.
The Engine
instance's scene_map
is an instance of the Map
class, just as the global a_map
is. In fact, a_game.scene_map
is the same instance as a_map
.
So, whatever you could do with a_map
at the top level, the Engine.play
code can do with self.scene_map
. It may be worth typing everything into the interactive interpreter up to the a_map
definition and playing around with a_map to make sure you know what exactly it can do for you.
So, why does Engine
need self.scene_map
? Why can't it just use the global a_map
?
Well, it could. The problem is that if you did that, you wouldn't be able to create two Engine
instances without them fighting over the same a_map
. (This is the same reason you don't want to use global variables in functions. Objects don't add a new problem—in fact, a big part of what objects are about is solving the global-variables problem.)
这篇关于困惑关于类在学习Python的硬的方式ex43?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!