苦学Python练习讲解:43

2024-04-23 12:13:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了这个要做的练习,但无法理解其中的一些代码。你知道吗

首先,我对流量控制感到困惑。我附加了一个屏幕截图有分裂的屏幕相同。在第二个屏幕有对象声明和调用方法屏幕截图还有我突出显示的编译输出。你知道吗

这是部分代码。你知道吗

class Engine(object):
    print 'Inside Engine class'
    def __init__(self, scene_map):
        self.scene_map = scene_map
        print 'scene_map is:',self.scene_map
    def play(self):
        print "inside play ",self
        flag = True
        current_scene = self.scene_map.opening_scene()
        if current_scene =="finish":
            flag = False
        while flag:
            print "\n--------"
            next_scene_name = current_scene.enter()
            if next_scene_name == "finish":
                exit(0)
            else:
                current_scene = self.scene_map.next_scene(next_scene_name)`

class Map(object):
    print "inside map class"
    scenes = {
            'central_corridor': CentralCorridor(),
            'laser_weapon_armory': LaserWeaponArmory(),
            'the_bridge': TheBridge(),
            'escape_pod': EscapePod(),
            'death': Death(),
            'finish':Finish()
            }
    def __init__(self, start_scene):
        print start_scene
        self.start_scene = start_scene
    def next_scene(self, scene_name):
        return Map.scenes.get(scene_name)
    def opening_scene(self):
        print "inside opening scene"
        return self.next_scene(self.start_scene)`
a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

输出:

Inside engine class
inside map class
Central_corridor
scene_map is: <__main__.Map object at 0x00000000029B7908>
inside play  <__main__.Engine object at 0x00000000029B7940>

1.首先用central_corridor作为参数调用Map()类。根据那行,它应该打印Inside the Map class。但是为什么我要得到Inside Engine class
什么是scene_map?对象或对象的引用??
谢谢。你知道吗


Tags: nameselfmapobject屏幕defscenestart
1条回答
网友
1楼 · 发布于 2024-04-23 12:13:36

Engine类是在Map之上定义的。你知道吗

打印的行写在类中,而不是在贡献者中。因此,只要将类加载到解释器,就会打印Inside class语句。你知道吗

相关问题 更多 >