gpt4 book ai didi

delphi - 使用继承的对象列表

转载 作者:行者123 更新时间:2023-12-03 18:13:52 24 4
gpt4 key购买 nike

我正在尝试创建一个对象结构,其中一个对象包含其他对象的列表。我对这两个都有一个基类,类似于 TCollection/TCollectionItem 但一个非常自定义的实现。

type
TMyItemBase = class;
TMyListBase = class;

TMyItemBaseClass = class of TMyItemBase;

TMyItemBase = class(TObject)
private
FOwner: TMyListBase;
public
constructor Create(AOwner: TMyListBase);
destructor Destroy; override;
property Owner: TMyListBase read FOwner;
end;

TMyListBase = class(TMyObjectBase)
private
FItems: TList;
FItemClass: TMyItemBaseClass;
function New: TMyItemBase;
function GetItem(Index: Integer): TMyItemBase;
public
constructor Create(AOwner: TMyMainObject; const ItemClass: TMyItemBaseClass);
destructor Destroy; override;
function Count: Integer;
procedure Clear;
property Items[Index: Integer]: TMyItemBase read GetItem; default;
end;

implementation

{ TMyItemBase }

constructor TMyItemBase.Create(AOwner: TMyListBase);
begin
FOwner:= AOwner;
end;

destructor TMyItemBase.Destroy;
begin

inherited;
end;


{ TMyListBase }

constructor TMyListBase.Create(AOwner: TMyMainObject; const ItemClass: TMyItemBaseClass);
begin
inherited Create(AOwner);
FItems:= TList.Create;
FItemClass:= ItemClass;
end;

destructor TMyListBase.Destroy;
begin
Clear;
FItems.Free;
inherited;
end;

procedure TMyListBase.Clear;
begin
while FItems.Count > 0 do begin
TMyItemBase(FItems[0]).Free;
FItems.Delete(0);
end;
end;

function TMyListBase.Count: Integer;
begin
Result:= FItems.Count;
end;

function TMyListBase.GetItem(Index: Integer): TMyItemBase;
begin
Result:= TMyItemBase(FItems[Index]);
end;

function TMyListBase.New: TMyItemBase;
begin
Result:= FItemClass.Create(Self);
FItems.Add(Result);
end;

(伪代码,如有错别字见谅)

问题是,当一个新项目被创建时(通过TMyListBase.New),对象被成功创建,但是继承和它的所有字段都没有(被继承的对象的构造函数甚至没有被调用)...

type
TMyItem = class;
TMyItems = class;

TMyItem = class(TMyItemBase)
private
//various unrelated fields
public
constructor Create;
destructor Destroy; override;
//various unrelated properties
end;

TMyItems = class(TMyListBase)
private
function GetItem(Index: Integer): TMyItem;
function New: TMyItem;
public
constructor Create(AOwner: TMyMainObject);
destructor Destroy; override;
property Items[Index: Integer]: TMyItem read GetItem; default;
end;

implementation

{ TMyItem }

constructor TMyItem.Create;
begin
inherited;
//Initialize some fields
end;

destructor TMyItem.Destroy;
begin
//Destroy some fields
inherited;
end;

{ TMyItems }

constructor TMyItems.Create(AOwner: TMyMainObject);
begin
inherited Create(AOwner, TMyItem);

end;

destructor TMyItems.Destroy;
begin

inherited;
end;

function TMyItems.GetItem(Index: Integer): TMyItem;
begin
Result:= TMyItem(inherited Channels[Index]);
end;

function TMyItems.New: TMyItem;
begin
Result:= TMyItem(inherited New);
end;

New 函数似乎有问题,但我无法弄清楚。即使我将项目创建为其预期的项目类,它也会被进一步视为基类,并且没有任何继承的成员可访问(导致访问冲突),因为从不调用继承的构造函数。

我在这里忽略了什么/做错了什么?

最佳答案

你的TMyItemBase.Create()构造函数需要被声明为virtual,然后后代类需要override它。这在使用元类类型构造对象时很重要。例如:

type
TMyItemBase = class(TObject)
...
public
constructor Create(AOwner: TMyListBase); virtual;
...
end;

constructor TMyItemBase.Create(AOwner: TMyListBase);
begin
inherited Create;
FOwner := AOwner;
end;

type
TMyItem = class(TMyItemBase)
...
public
constructor Create(AOwner: TMyListBase); override;
...
end;

constructor TMyItem.Create(AOwner: TMyListBase);
begin
inherited Create(AOwner);
...
end;

关于delphi - 使用继承的对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20694894/

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