简体   繁体   English

艰苦学习Python-练习43-类:实例化时将一个类实例传递给另一个类

[英]Learn Python the Hard Way - exercise 43 - Classes: passing one class instance to another class when instantiating

From Learn Python the Hard Way, 3rd Edition - exercise 43: 摘自“学习Python的艰难方法”,第三版-练习43:

In the code below, a Map object instance is created named a_map and Engine object instance is created named a_game. 在下面的代码中,创建了一个名为a_map的Map对象实例,并创建了一个名为a_game的Engine对象实例。

  1. In passing a_map as a variable during a_game instantiation is this code setting a_game.scene_map as an object variable for the entire a_map object? 在a_game实例化期间将a_map作为变量传递时,此代码是否将a_game.scene_map设置为整个a_map对象的对象变量?

  2. Does this provide the essentials of inheritance functionality to the a_game instance of Engine without using subclass inheritance? 这是否在不使用子类继承的情况下为Engine的a_game实例提供了继承功能的要点?

Given that a_game.play() calls self.scene_map.opening_scene() successfully from the a_map instance I'm assuming both of the above questions' answers are 'yes' but it seems like there's a bit of simplified hand waving magic here for something pretty complicated. 假设a_game.play()从a_map实例成功调用了self.scene_map.opening_scene(),我假设上述两个问题的答案均​​为“是”,但似乎这里有些简化的挥手魔术了相当复杂。

I'd like to read more about this topic but most of what I've come across on classes has dealt with more straight forward examples. 我想阅读有关此主题的更多信息,但是我在课堂上遇到的大多数内容都涉及了更直接的示例。

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

Well, look at what's happening. 好吧,看看发生了什么。

a_map = Map('central_corridor')
a_game = Engine(a_map)

So, we're creating a map, and passing that map to the Engine. 因此,我们正在创建一个地图,并将该地图传递给引擎。 What does that do? 那是做什么的? Depends on what Engine.__init__ does with it. 取决于Engine.__init__执行的操作。

class Engine(object):

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

Stores the map as an attribute of the engine instance, so that other methods of the engine can use the map. 将地图存储为引擎实例的属性,以便引擎的其他方法可以使用地图。

You've got this far already, seems like. 您已经走了这么远,似乎。 So the answers to your questions are: 因此,您的问题的答案是:

  1. Yes. 是。
  2. No. The Engine instance has a Map . Engine实例具有 Map It is not, in itself, a map, nor does it behave like one (ie its interface is not like Map ). 它本身不是地图,也不像一个地图(即其界面不像Map )。

To delve further into this question, why aren't you asking the same question about Map ? 为了进一步探讨这个问题,您为什么不问有关Map的相同问题? After all, it takes a data element at instantiation and stores it as an attribute, so that other methods of Map have access to it. 毕竟,它在实例化时需要一个数据元素并将其存储为属性,以便Map其他方法可以访问它。

In short: the Map instance is a data element to Engine the same way the str instance is a data element to Map . 简而言之: Map实例是Engine的数据元素,就像str实例是Map的数据元素一样。 The engine needs the map to play the game, but the engine is not a map. 引擎需要地图才能玩游戏,但是引擎不是地图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM