其实还可以更细些 type TB=class(TObject) private: public: Lists:Array of TA; function add;//用于增加一个listitem,在这里对lists和lstcnt进行分配内存和增一工作 function delete;//用于减一个listitem,在这里对lists和lstcnt进行释放内存 lstCnt:integer; end;
[code] TB = class (TObject) private FTAs:array [1..1oo] of TA; public function GetTA(index:integer):TA; procedure SetTA(i:integer; const value:TA); property Lists[i:integer] :TA read GetTA write SetTA; end;procedure TB.SetTA(i:integer; const value:TA) begin FTAs[i]:=TA; end;function TB.GetTA(index:integer):TA; begin Result:=FTAs[index]; end;我是这样写的,但赋值时不对 TB.Lists[2].ID = 'A01'; TB.Lists[2].Name = 'ABC'; 原因是TA类没有建立。 怎么写,给代码一定给分!
你要先建立TA类,再加两个成员函数: procedure TB.CreateTAClass(cnt:integer);//cnt 生成的个数 var i:integer; begin if cnt>0 then begin lstCnt:=cnt; SetLength(Lists,cnt); for i:=0 to cnt-1 do begin Lists[i]:=TA.Create; end; end; end; procedure TB.FreeTAClass; var i:integer; begin for i:=0 to lstCnt-1 do begin if Lists[i]<>nil then Lists.Free; end; end;
type
TB=class(TObject)
private:
public:
Lists:Array of TA;
function add;//用于增加一个listitem,在这里对lists和lstcnt进行分配内存和增一工作
function delete;//用于减一个listitem,在这里对lists和lstcnt进行释放内存
lstCnt:integer;
end;
TB = class (TObject)
private
FTAs:array [1..1oo] of TA;
public
function GetTA(index:integer):TA;
procedure SetTA(i:integer; const value:TA);
property Lists[i:integer] :TA read GetTA write SetTA;
end;procedure TB.SetTA(i:integer; const value:TA)
begin
FTAs[i]:=TA;
end;function TB.GetTA(index:integer):TA;
begin
Result:=FTAs[index];
end;我是这样写的,但赋值时不对
TB.Lists[2].ID = 'A01';
TB.Lists[2].Name = 'ABC';
原因是TA类没有建立。
怎么写,给代码一定给分!
procedure TB.CreateTAClass(cnt:integer);//cnt 生成的个数
var
i:integer;
begin
if cnt>0 then
begin
lstCnt:=cnt;
SetLength(Lists,cnt);
for i:=0 to cnt-1 do
begin
Lists[i]:=TA.Create;
end;
end;
end;
procedure TB.FreeTAClass;
var
i:integer;
begin
for i:=0 to lstCnt-1 do
begin
if Lists[i]<>nil then
Lists.Free;
end;
end;
内部维持一TList
加一数组属性,