pascal - Pascal-两个不同文件之间的类继承?

标签 pascal lazarus freepascal delphi

说我有两个文件,characters.pasogre.pas。食人魔是一个角色,但为了干净起见,我试图将两个文件分开。在characters.pas中,我有

unit Characters;

{$mode objfpc}{$H+}

interface
type
  TCharacter = class(TOBject)
    private
      // ...
    public
      // ...
    published
      // ...
  end;

implementation
  // Method bodies
end.


ogre.pas中,我有

unit Ogre;

{$mode objfpc}{$H+}

interface
type
  TOgre = class(TCharacter)
    public
        constructor create; override;
    end;


implementation

constructor TOgre.create();
begin
  // Banana banana banana
end;
end.


在任何一个.pas文件中的任何位置添加uses块都会引发错误,这使我相信依赖于继承的所有类都必须与其父类位于同一文件中。我想念什么吗?

最佳答案

是的,您错过了一些东西:use部分。您必须声明unit Ogre使用unit Characters

食人魔单位;

{$mode objfpc}{$H+}

interface

uses
  Characters;

type
  TOgre = class(TCharacter)
    public
        constructor create; override;
    end;


implementation

constructor TOgre.create();
begin
  // Banana banana banana
end;
end. 


阅读更多:


unit example @FPC
the use of a second form @FPC wiki


还要注意,如果希望某些字段从TCharacterTOgre可见,但仍不能从主程序访问,则必须将其可见性设置为protected

关于pascal - Pascal-两个不同文件之间的类继承?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32174245/

相关文章:

arrays - 创建随机二维数组 (4x4) FreePascal

png - 在 TGLCompositeImage 中添加 PNGImage 时出现 SIGSEGV 错误

Delphi BASM 代码在针对 Win64 时出现错误。它可以转换为纯帕斯卡吗?

delphi - 我应该引用字段或属性名称吗?

delphi - 更改进度栏颜色

multithreading - WaitForThreadTerminate超时时返回什么错误代码?

syntax-error - 错误: illegal Expression

algorithm - 实数的幂运算

Pascal - 读/读函数杂质?

c++ - 如何在 Lazarus (freepascal) 中使用 C++ 生成的 .dll 或 .lib 文件中定义的函数?