TStrings 中存储 object(50分)

  • 主题发起人 主题发起人 MengZhaoXing
  • 开始时间 开始时间
M

MengZhaoXing

Unregistered / Unconfirmed
GUEST, unregistred user!
TStrings 中存储 object
我在 TStrings 中存储子定义object时 addobjects('anystring',TObject(myobject))
出错,"abstract error", 谁能帮我?

源吗如下:

////////////////////////////////

unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Edit2: TEdit;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TFlow = class(TComponent)
private
procedure ReadFlow(Reader: TReader);
procedure WriteFlow(Writer: TWriter);
public
Name: String;
list: TStrings;
constructor create(component: TComponent)
override;
destructor destroy
override;
protected
procedure DefineProperties(Filer: TFiler)
override;
end;

PTempList = ^TTempList;
TTempList = class(TComponent)
private
procedure ReadTemp(Reader: TReader);
procedure WriteTemp(Writer: TWriter);
public
Name: String;
constructor create(component: TComponent)
override;
destructor destroy
override;
protected
procedure DefineProperties(Filer: TFiler)
override;
end;


var
Form1: TForm1;
Flow: TFlow;

implementation

{$R *.DFM}

constructor TTempList.create(component: TComponent);
begin
inherited Create(component);
name := '';
end;

destructor TTempList.destroy;
begin
inherited destroy;
end;

procedure TTempList.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('TempInfo',ReadTemp, WriteTemp, true);
end;

procedure TTempList.ReadTemp(Reader: TReader);
begin
Name := Reader.ReadString;
end;

procedure TTempList.WriteTemp(Writer: TWriter);
begin
Writer.WriteString(Name);
end;

constructor TFlow.create(component: TComponent);
begin
inherited create(component);
List := TStrings.Create;
end;

destructor TFlow.destroy;
begin
List.Free;
inherited destroy;
end;

procedure TFlow.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('FlowInfo',ReadFlow, WriteFlow, true);
end;

procedure TFlow.ReadFlow(Reader: TReader);
begin
Name := Reader.ReadString;
Reader.Read(list,sizeOf(List));
end;

procedure TFlow.WriteFlow(Writer: TWriter);
begin
Writer.WriteString(name);
Writer.Write(List,SizeOf(List));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Flow := TFlow.Create(self);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
FileStream: TFileStream;
temp: TTempList;
begin
try
temp := TTempList.Create(self);
temp.Name := Edit2.Text;
Flow.Name := Edit1.Text;
------------------------------------------------------------
Flow.list.AddObject('tmplist',TObject(temp));

--------- ??????运行时出错,'abstract error'------------------

FileStream := TFileStream.Create('c:/flow.dat',fmCreate);
FileStream.WriteComponent(Flow);
finally
FileStream.Free;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
FileStream: TFileStream;
temp: PTempList;
begin
try
new(temp);
Temp^ := TTempList.Create(self);
Flow.destroy;
Flow := TFlow.Create(self);
Flow.Name := '';
FileStream := TFileStream.Create('c:/flow.dat',fmOpenRead);
FileStream.ReadComponent(Flow);
temp^ := TTempList(Flow.list.objects[0]);
Edit2.Text := Temp^.Name;
Edit1.Text := Flow.Name;
finally
FileStream.Free;
dispose(temp);
end;
end;

end.
 
问题出在这外地方:

//constructor TFlow.create(component: TComponent);
//begin
// inherited create(component)

// List := TStrings.Create
// error
//end;

TStrings是一个抽象类,不能直接创建它的实例,应该创建它的子类(非抽象类)的实例,如TStringlist等.
你的问题只须改一个地方:
把 List := TStrings.Create;
改为 List := TStringlist.Create;
(List变量的声名不须改动)
 
应该用TStringList代替TStrings, 因为TStrings是一个基本类, 有一些方法过程
是Abstract没有实际代码, 而TStringList中有.
 
另外,TempList本来就是指针,没必要用PTempList.
 
abstract 抽象
用TStringList的构造方法!
 
多人接受答案了。
 

Similar threads

后退
顶部