【问题标题】:Put classes in static arrays of other classes将类放入其他类的静态数组中
【发布时间】:2015-03-16 07:04:19
【问题描述】:

这是一个非常具体的问题,很难解释。但这是我的代码的一部分:

class Type:
    color      = [168, 168, 120]
    weakness   = []
    resistance = []
    immunity   = []

#**************************************************************************
#Defines all types---------------------------------------------------------
#**************************************************************************        
class Normal(Type):
    color      = [168, 168, 120]
    weakness   = [Fighting]
    resistance = []
    immunity   = [Ghost]

class Fighting(Type):
    color      = [192, 48, 40]
    weakness   = [Flying, Psychic, Fairy]
    resistance = [Rock, Bug, Dark]
    immunity   = []

class Flying(Type):
    color      = [168, 144, 240]
    weakness   = [Rock, Electric, Ice]
    resistance = [Fighting, Bug, Grass]
    immunity   = [Ground]

是的,这些是口袋妖怪类型,这些只是我文件中 18 个中的 3 个,但它们本质上都是相同的。我想要做的是让所有这些类都有这些其他类的静态数组。

问题是 Normal.weakness 数组中的 Fighting 给出错误,因为 Fighting 尚未声明。不过 Flying.resistance 中的战斗很好,因为战斗已经宣布。

有没有简单的解决方法?我尝试的第一件事是让类看起来像这样:

class Fighting(Type):
    def __init__(self):
        self.color      = [192, 48, 40]
        self.weakness   = [Flying, Psychic, Fairy]
        self.resistance = [Rock, Bug, Dark]
        self.immunity   = []

但是每当我想实现这些类时,我都必须为它们创建一个实例,这很烦人。

我考虑过声明所有类,然后定义所有数组,例如 Normal.resistance = [战斗] 但这似乎有点麻烦。我也想过将它们全部制作成单独的文件并将它们相互导入,但我什至不知道这是否可行。如果有人可以在这里帮助我,我将不胜感激!

--编辑--

我最终把它变成了一个带有获取数组的函数的枚举,这样更有意义

【问题讨论】:

  • 一般来说,您不会存储类数组(您正在那里进行元编程),您将存储标识符数组,如字符串或枚举,并在您的逻辑中使用它们。类用于定义其对象实例的行为,而不是在逻辑中显式使用。除非这是我不知道的 Python 特质。
  • 可能先定义所有类,列表为空。然后用所需的值填充类属性。

标签: python arrays class static


【解决方案1】:

这个怎么样:

class Type:
    @classmethod
    def classInit(cls):
            if not hasattr(cls, color):
                cls.color = [168, 168, 120]
                cls.weakness = []
                cls.resistance = []
                cls.immunity   = []
    def __init__(self):
        self.classInit()  #only does something the first time it is called
class Normal(Type):
    ...
class Flying(Type):
    ...

【讨论】:

    【解决方案2】:

    我建议有一个名为 species 的文件夹(又名包)('type' 是 Python 中的保留字,所以我想避免以任何形式使用它),在 species 文件夹中你有一个每个物种的单独文件(又名模块)。

    通过这种方式,您可以毫无问题地导入任何特定物种的弱点、抵抗力和免疫力的物种。

    如果你不喜欢species,其他一般的类似群组的词是kindcategoryvarietybreedclassification,仅举几例.

    文件夹示例

    /species
        __init__.py
        abstract.py (what you call `class Type` I would rename to AbstractSpecies)
        normal.py  (from . import Fighting, Ghost)
        fighting.py  (from . import ...)
        flying.py  (from . import ...)
    

    文件示例(normal.py)

    from .abstract import AbstractSpecies
    from .fighting import Fighting
    from .ghost import Ghost
    
    
    class Normal(AbstractSpecies):
        color      = [168, 168, 120]
        weakness   = [Fighting]
        resistance = []
        immunity   = [Ghost]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-26
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 1970-01-01
      相关资源
      最近更新 更多