就像 Ozan 说的:大多数时候,一个好的答案是使用虚拟方法创建一个基类:
unit BaseWorld;
TBaseWorld = class
function GetWorldInfo() : TWorldInfo; virtual; {abstract;}
...
unit Player;
TPlayer = class
FWorld : TBaseWorld;
constructor Create( AWorld : TBaseWorld );
...
unit RealWorld;
TWorld = class(TBaseWorld)
function GetWorldInfo() : TWorldInfo; override;
...
TWorld.AddPlayer();
begin
TPlayer.Create(Self);
end;
...
或者,具有类似的效果,发布一个界面:
unit WorldIntf;
IWorldInterface = interface
function GetWorldInfo() : TWorldInfo;
...
unit Player;
TPlayer = class
FWorld : IWorldInterface;
constructor Create( AWorld : IWorldInterface );
...
unit RealWorld;
TWorld = class(TInterfacedObject, IWorldInterface)
function GetWorldInfo() : TWorldInfo;
...
TWorld.AddPlayer();
begin
TPlayer.Create(Self);
end;
...
根据您的代码的工作方式,您可能希望将世界隐藏在抽象层(如上面的示例中)或播放器(如 Ozan 建议)后面。