如何覆盖TIniFile.Create?

WeG*_*ars 2 delphi

如何覆盖TIniFile.Create构造函数?

此代码无效,因为Create是静态的:

  TMyIniFile = class(TIniFile)
   protected
   public
     constructor Create  (CONST AppName: string); override;  <------ Error 'Cannot override a non-virtual method' 
   end;
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 6

您不能覆盖构造函数,TIniFile因为它不是虚拟的.ini文件类不使用虚拟构造函数.

您只需override要从代码中删除它.

TMyIniFile = class(TIniFile)
public
  constructor Create(const AppName: string);
end;
Run Code Online (Sandbox Code Playgroud)

像这样实现它:

constructor TMyIniFile.Create(const AppName: string);
begin
  inherited Create(FileName);//I can't tell what you need as FileName
  FAppName := AppName;
end;
Run Code Online (Sandbox Code Playgroud)

当你需要创建一个时,你这样做:

MyIniFile := TMyIniFile.Create(MyAppName);
Run Code Online (Sandbox Code Playgroud)

  • 这正是`TMemIniFile`所做的. (2认同)