如何访问Delphi中的基(超)类?

Niy*_*wan 10 delphi inheritance

在C#中我可以通过base关键字访问基类,在java中我可以通过super关键字访问它.如何在delphi中做到这一点?假设我有以下代码:

  type
    TForm3 = class(TForm)
  private
    procedure _setCaption(Value:String);
  public
    property Caption:string write _setCaption; //adding override here gives error
  end;

  implementation


procedure TForm3._setCaption(Value: String);
begin
  Self.Caption := Value; //it gives stack overflow      
end;
Run Code Online (Sandbox Code Playgroud)

RRU*_*RUZ 13

您正在获得stackoveflow异常,因为该行

Self.Caption := Value;
Run Code Online (Sandbox Code Playgroud)

是递归的.

您可以访问将属性Caption转换Self为基类的父属性,如下所示:

procedure TForm3._setCaption(const Value: string);
begin
   TForm(Self).Caption := Value;
end;
Run Code Online (Sandbox Code Playgroud)

或使用inherited关键字

procedure TForm3._setCaption(const Value: string);
begin
   inherited Caption := Value;
end;
Run Code Online (Sandbox Code Playgroud)


da-*_*oft 11

你应该使用inherited关键字:

procedure TForm3._setCaption(Value: String); 
begin 
  inherited Caption := Value;
end;
Run Code Online (Sandbox Code Playgroud)