如何将多个类有效地链接在一起 [英] How to Link Multiple Classes Together Efficiently

查看:180
本文介绍了如何将多个类有效地链接在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我一直在用Python进行游戏,为了使所有内容保持整洁,井井有条且以pythonic的方式,我有一个文件夹,其中包含多个python文件,每个文件包含一个大类,用于例如 MapEngine或 NPCEngine。

Background: I have been working on a game in Python, and in order to keep everything clean, organized and in a pythonic way, I have a folder containing multiple python files, each containing one big class, for example "MapEngine" or "NPCEngine".

从main.py中,我从每个文件加载每个类,并将所有内容与游戏类粘合在一起,例如:

From main.py, I am loading each class from each file and "glueing everything together with a "Game" class, such as:

from folder import *

class Game:

    def __init__(self):
        self.MapEngine = MapEngine.MapEngine()
        ...

    def loop(self):
        ...

由于诸如 CollisionEngine之类的类需要来自其他类(例如 MapEngine)的数据,因此我通常将前者(即CollisionEngine)中的一些变量分配给后者(即MapEngine),以便使用MapEngine加载的地图数据或函数:

Since classes such as "CollisionEngine" requires data from other classes such as, "MapEngine", I usually assign some variables in the former (i.e. CollisionEngine) to the latter (i.e MapEngine), in order to use MapEngine's loaded map data or functions:

class CollisionEngine:

    def __init__(self, MapClass, ...):
        self.MapEngine = MapClass

问题
好​​吧,由于许多类必须链接到其他类,因此过一会儿就很难弄清楚首先要加载哪个类才能分配变量s。此外, EventEngine之类的类需要访问其他所有类。我的代码变得难以阅读,当两个类彼此同等重要时,我会遇到麻烦。

Problem: Well, since many classes have to be linked to others, it became hard after a while to figure out which class to load first in order to assign variables. Furthermore, classes like "EventEngine" need to have access to every other class. My code became hard to read, and I have trouble when 2 classes are equally important to each other.

问题
我我听说过类继承,但是我认为它不能在这里应用,因为每个类的功能都非常不同。因此,是否有一种方法可以将每个班级完美地联系在一起,好像它们都属于一个大班级一样?换句话说,有没有办法从一个类中引用其他类的变量?

Question: I have heard of class inheritance, but I do not think it can be applied here because each class is very different as in its function. Therefore, is there a way to beautifully link every class together, as if it was all part of one big class? In other words, is there a way to refer to variables from other classes, from within a class?

(我的想法:也许,我可以编写一个名为 Request的类,它将充当顶级类管理器。尽管,我认为我将不得不使用exec()或eval()之类的函数,这些函数效率不高且有些危险。)

(My thoughts: Perhaps, I can write a class called "Request", and it will act as a top level class manager. Although, I think I will have to use functions such as exec() or eval(), which are not efficient and are somewhat dangerous.)

这是我的第一篇文章,我试图尽可能地明确,请让我澄清一下&谢谢您的答复!

This is my first post, I've tried to be as explicit as possible, please ask me for clarification, & thank you for your reply!

推荐答案

好吧,经过多次尝试,我找到了解决问题的一种简单方法。当然,如eddiewould所建议的那样,我将拥有一个更好的组织结构和多层代码,但是如果希望将多个类都链接在一起,则只需在每个类中包含一个主类变量(称为每个类) 。我相信一个代码片段会更好地解释它:

Well, after many tries, I have figured out a (sketchy) way of solving my problem. Of course, as eddiewould suggested, I will have a better organization and multiple layers for my code, but if one would like to have multiple classes all linked together, simply include a variable to the main class (that called every class) to every class. I believe that a code snippet will explain it better:

main.py
engine_folder
----> engine_1.py
----> engine_2.py

在main.py中,加载了engine_1和engine_2:

in main.py, engine_1 and engine_2 are loaded:

from engine_folder import engine_1, engine_2

class game:

  def __init__(self):
      self.engine_1 = engine_1.engine(self, ...)
      self.engine_2 = engine_2.engine(self, ...)
      #where engine_1.engine and engine_2.engine are
      #two classes that need to transfer data between
      #each other

  def run(self):
      self.engine_1.run()
      self.engine_2.run()

请注意engine_1.engine的第一个参数是self,它是顶级类叫这个班。现在,在engine_1中,如果我们要从engine_2打印一个变量,则该类将类似于以下内容:

Notice how engine_1.engine's first argument is self, which refers to the top level class which called this class. Now, in engine_1, if we would want to print a variable from engine_2, the class would look similar to this:

class engine:

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

  def run(self):
      print self.top_level_class.engine_2.random_var

这非常漂亮(除了打印出self.top_level_class.engine_2的事实.random_var很长),但与以下内容相比:

This is very beautiful (besides the fact that print self.top_level_class.engine_2.random_var is very long), but compared to something like:

class EventEngine:

  def __init__(self, Camera_Class, Map_Class, Character_Class, Skill_Class,
               Interface_Class, Sprite_Class, Dialog_Class, Game_Class,
               Item_Class):
      self.ItemEngine = Item_Class
      self.GameEngine = Game_Class
      self.DialogEngine = Dialog_Class
      self.SpriteEngine = Sprite_Class
      self.SkillEngine = Skill_Class
      self.CameraEngine = Camera_Class
      self.MapEngine = Map_Class
      self.CharacterEngine = Character_Class
      self.IEngine = Interface_Class

新版本:

class EventEngine:
  def __init__(self, top_level_class):
      self.top = top_level_class
      #a var from Map_Class can be called as such:
      #self.top.MapEngine.map[0][1]
      #and I can call variables from every class, not only those I 
      #have loaded like before

之前更好,更清洁。

这篇关于如何将多个类有效地链接在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆