这是一个非常具体的问题,很难解释。但这是我代码的一部分:
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]
是的,这些是Pokemon类型,它们只是我文件中18个类型中的3个,但它们基本上是相同的。我要做的是让所有这些类都有其他类的静态数组。你知道吗
问题是战争正常。虚弱数组出现错误,因为战斗尚未声明。然而,战争飞行阻力很好,因为已经宣布了战斗。你知道吗
有什么简单的解决办法吗?我尝试的第一件事就是让课程看起来像这样:
class Fighting(Type):
def __init__(self):
self.color = [192, 48, 40]
self.weakness = [Flying, Psychic, Fairy]
self.resistance = [Rock, Bug, Dark]
self.immunity = []
但是,每当我想实现这些类时,我就必须为它们创建一个实例,这很烦人。你知道吗
我考虑过声明所有的类,然后定义所有的数组,比如 正常电阻=[战斗] 但这似乎有点麻烦。我也想过把它们都分开,然后互相导入,但我甚至不知道这样做是否行得通。如果有人能帮我,我会非常感激的!你知道吗
--编辑--
最后我把它做成了一个枚举,带有获取数组的函数,这样做更有意义
我建议使用一个名为
species
(“type”在Python中是一个保留字,所以我希望避免以任何形式使用它),并且在species
文件夹中,每个物种都有一个单独的文件(aka,module)。你知道吗通过这种方式,你可以导入特定的物种的弱点,抵抗力和免疫力为任何给定的物种没有问题。你知道吗
如果你不喜欢
species
,其他一般的类词有kind
、category
、variety
、breed
和classification
,这是我脑子里想的几个词。你知道吗文件夹示例
文件示例(正常.py)
这个怎么样:
相关问题 更多 >
编程相关推荐