gpt4 book ai didi

delphi - 如何覆盖继承的属性?

转载 作者:行者123 更新时间:2023-12-02 04:30:05 25 4
gpt4 key购买 nike

我有一个类(TMyClass),它有一个属性(Items:TItems)

TItems = class;    

TMyClass = class(TComponent)
private
FItems: TItems;
procedure SetItems(const Value: TItems);
protected

public

protected
property Items: TItems read FItems write SetItems;
end.

TExItems = class(TItems)
private
FNewProb: Integer;
protected

public

published
property NewProp: Integer read FNewProb write FNewProb;
end.

TExMyClass = class(TMyClass)
private
FItems: TExItems;
procedure SetItems(const Value: TItems);
protected

public

published
property Items: TExItems read FItems write SetItems;
end.

新的“Items”属性是从TItems继承的,但是当我安装组件时,TExItems的新属性“NewProb”没有出现,看起来“Items”属性仍然是TItems而不是TExItems...如何覆盖它?

谢谢

修改:这是真正的代码

类型 TKHAdvSmoothDock = 类;

TKHAdvSmoothDockItem = class(TAdvSmoothDockItem)
private
FImageIndex: TImageIndex;
procedure SetImageIndex(const Value: TImageIndex);
protected

public

published
property ImageIndex: TImageIndex read FImageIndex write SetImageIndex default -1;
end;

TKHAdvSmoothDockItems = class(TAdvSmoothDockItems)
private
FOwner: TKHAdvSmoothDock;
FOnChange: TNotifyEvent;
function GetItem(Index: Integer): TKHAdvSmoothDockItem;
procedure SetItem(Index: Integer; const Value: TKHAdvSmoothDockItem);
protected
function GetOwner: TPersistent; override;
public
constructor Create(AOwner: TKHAdvSmoothDock);
function Add: TKHAdvSmoothDockItem;
function Insert(Index: Integer): TKHAdvSmoothDockItem;
property Items[Index: Integer]: TKHAdvSmoothDockItem read GetItem write SetItem; default;
procedure Delete(Index: Integer);
published
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;

TKHAdvSmoothDock = class(TAdvSmoothDock)
private
FImageChangeLink: TChangeLink;
FImages: TCustomImageList;
FItems: TKHAdvSmoothDockItems;
procedure ImageListChange(Sender: TObject);
procedure SetImages(const Value: TCustomImageList);
procedure SetItems(const Value: TKHAdvSmoothDockItems);
function GetItems: TKHAdvSmoothDockItems;
{ Private declarations }
protected
procedure UpdateImagesFromImageList;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Images: TCustomImageList read FImages write SetImages;
property Items: TKHAdvSmoothDockItems read GetItems write SetItems;
end;

问候。

最佳答案

属性 getter 和 setter 可以是虚拟的,然后通过继承类覆盖,请参阅下面的更新示例。您的示例代码有一个警告,那就是您正在尝试更改属性的类型,这是不允许的。我建议您检查 TExMyClass.SetItems 中的 Value is TExItems 但使用继承的 Items 属性并在 TExMyClass 的所有方法中强制转换为 TExItems 和更多继承人。

TItems = class;

TMyClass = class(TComponent)
private
FItems: TItems;
procedure SetItems(const Value: TItems); virtual;
protected
property Items: TItems read FItems write SetItems;
end;

TExItems = class(TItems)
private
FNewProb: Integer;
protected

public

published
property NewProp: Integer read FNewProb write FNewProb;
end;

TExMyClass = class(TMyClass)
private
procedure SetItems(const Value: TItems); override;
end;

关于delphi - 如何覆盖继承的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11432942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com