python - "Moving"在Python中从一个类到另一个类

标签 python oop

所以,我正在解决这个问题,如果有人能指出我正确的方向,我将非常感激。让我为您设置一下。

我有 1 个名为 rooms 的 Python 文件/模块,其中充满了类,如下所示:

class Intro(object):

    def __init__(self):
         # Setting up some variables here

    def description(self):
        print "Here is the description of the room you are in"

哪一个既酷又对?然后在另一个名为 Engine 的文件/模块上。我有这个:

import rooms

class Engine(object):

    def __init__(self, first_class):
        self.lvl = first_class

    def play_it(self):
        next_lvl = self.lvl

        while True:
            # Get the class we're in & run its description
            next_lvl.description() # it works.
            # But how do I get the next class when this class is done?

看看我想要发生的情况是基于用户在每个房间/级别类别中的决定,引擎调用一个新类别及其描述功能/属性。那有意义吗?或者我应该以另一种方式思考这个问题?我有时会倒退。谢谢。

最佳答案

将接下来使用哪个类的选择委托(delegate)给房间。例如

class Intro(object):

    def __init__(self):
         # Setting up some variables here

    def description(self):
        print "Here is the description of the room you are in"

    def north(self):
        return Dungeon()

    def west(self):
        return Outside()

    #etc

因此,当玩家在 Intro 房间中说“向西走”时,引擎会调用 room.west() 并且 Intro 房间返回 Outside 房间。

希望这已经足够了!我希望您会想要制作自己的设计。

关于python - "Moving"在Python中从一个类到另一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10566193/

相关文章:

python - 从类实例中访问静态方法的正确/首选方法

python - 在 Windows XP 上运行 Python CGI 脚本

python - 在运行时发现 Java .jar 中的 Jython 模块?

python - python 如何以不同的时间间隔递增?

java - 如何将参数传递给静态初始化 block

java - 如何使用构造函数设置不在构造函数内部的其他字段

java - 即使对象属于子类,也会调用父类(super class)方法

python - plotly :如何从具有长格式或宽格式的 Pandas 数据框中绘制线图?

python kivy "key_error: pos_hint"

ios - 让一个 View Controller 听另一个