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

如何在Delphi中访问基(超)类?,delphi,inheritance,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

在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;

您将收到stackoveflow异常,因为该行

Self.Caption := Value;
是递归的

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

procedure TForm3._setCaption(const Value: string);
begin
   TForm(Self).Caption := Value;
end;
或者使用继承的
关键字

procedure TForm3._setCaption(const Value: string);
begin
   inherited Caption := Value;
end;

您应该使用
继承的
关键字:

procedure TForm3._setCaption(Value: String); 
begin 
  inherited Caption := Value;
end;
base(C#)=super(java)=继承的(Object Pascal)(*)

这3个关键字的工作方式相同

1) 调用基类构造函数
2) 调用基类方法
3) 为基类属性赋值(假设它们不是私有的,只允许受保护的和公共的)
4) 调用基类析构函数(对象仅限于Pascal.C,Java没有析构函数)


(*)Object Pascal比Delphi或Free Pascal更可取,因为Object Pascal是程序语言的名称,而Delphi和Free Pascal是Object Pascal的编译器