问题描述
这是我计划用于游戏的代码。但它抱怨MRO错误。我不知道为什么。有人能为我解释一下吗?非常感谢。
This is the code which I plan to use for my game. But it complains for an MRO error. I don't know why. Can someone explains for me? Many thanks.
class Player:
pass
class Enemy(Player):
pass
class GameObject(Player, Enemy):
pass
g = GameObject()
推荐答案
您的 GameObject
继承自玩家
和 敌人
。因为 Enemy
已经继承自 Player
Python现在无法确定要查找方法的类第一; 播放器
,或 Enemy
,它将覆盖播放器$ c中定义的内容$ c>。
Your GameObject
is inheriting from Player
and Enemy
. Because Enemy
already inherits from Player
Python now cannot determine what class to look methods up on first; either Player
, or on Enemy
, which would override things defined in Player
.
您不需要在此处命名 Enemy
的所有基类;只从这一个类继承:
You don't need to name all base classes of Enemy
here; just inherit from that one class:
class GameObject(Enemy):
pass
Enemy
已包含播放器
,你不需要再包括它。
Enemy
already includes Player
, you don't need to include it again.
这篇关于TypeError:无法创建一致的方法解析顺序(MRO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!