首页 / PYTHON / 学习Python艰难的练习43
学习Python艰难的练习43
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了学习Python艰难的练习43,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2919字,纯文字阅读大概需要5分钟。
内容图文
![学习Python艰难的练习43](/upload/InfoBanner/zyjiaocheng/762/6479905ef654411e8cad0e3169c0ed1f.jpg)
我不明白脚本如何获得下一个房间,以及“引擎”和“地图”类的工作原理.这是一段摘录:
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().事实上,这就是我在阅读答案之前解决它的方法,并且结果很好.
对不起,我的主要问题是引擎和地图类的工作原理.另一件事是次要的.
提前致谢!
解决方法:
Map对象是一个映射场景的Class.它有一些场景保存在一个数组中.
scenes = {
'central_corridor': CentralCorridor(),
'laser_weapon_armory': LaserWeaponArmory(),
'the_bridge': TheBridge(),
'escape_pod': EscapePod(),
'death': Death()
}
当制作一个Map对象时,你也会给它一个开头场景,如构造函数中所示
def __init__(self, start_scene):
self.start_scene = start_scene
这会在Map中创建一个名为start_scene的变量,其中包含您的开场景.
此外,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)
引擎似乎在控制场景何时播放以及播放什么.
# 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)
why is it required to have these two classes anyway?
除非根据您的任务要求,否则不是
就像你说的那样可以没有,但是有充分的理由这样做.
通过这种方式,您可以使用自己的职责制作2个单独
这种方式代码在应用程序变得越来越大时更具可读性.并且可以轻松浏览应用程序.您可以轻松更改应用程序的部分内容等.我的建议是继续练习和阅读有关OOP的内容,您会注意到为什么要执行您所看到的内容.
内容总结
以上是互联网集市为您收集整理的学习Python艰难的练习43全部内容,希望文章能够帮你解决学习Python艰难的练习43所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。