在Delphi中如何访问base(super)类? [英] How to access base (super) class in Delphi?
本文介绍了在Delphi中如何访问base(super)类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在C#中,我可以通过 base
关键字访问基类,在java中,我可以通过 super
关键字。在delphi中怎么做?
假设我有以下代码:
In C# i can access base class by base
keyword, and in java i can access it by super
keyword. How to do that in delphi?
suppose I have following code:
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异常,因为行
You are getting a stackoveflow exception because the line
Self.Caption := Value;
是递归的。
父属性 Caption
投射 Self
属性到基类,如下所示:
You can access the parent property Caption
casting the Self
property to the base class like so :
procedure TForm3._setCaption(const Value: string);
begin
TForm(Self).Caption := Value;
end;
或使用继承
关键字
procedure TForm3._setCaption(const Value: string);
begin
inherited Caption := Value;
end;
这篇关于在Delphi中如何访问base(super)类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文