100求助高手,动态类实例生成的问题(分不够可以再开贴)

applebomb 2006-09-21 01:23:21
希望能通过传入类名参数或者类参考实例也好,反正在函数中能够生成对应的类实例。老是显示抽象类错误,快郁闷死了。。。。。能有classforname该多好啊(该死的DELPHI)。

有什么好的办法

代码如下:
-------------
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
list: TList;
{ Private declarations }
public
{ Public declarations }
end;

C_BASE = class(TObject)
public
constructor create; virtual; abstract;
procedure doit; virtual; abstract;
end;
classC = class of C_BASE;

C_A = class(C_BASE)
private
ii: Integer;
public
aa: string;
a0: string;
constructor create; overload;
procedure doit; override;
end;
classA = class of C_A;

C_B = class(C_BASE)
public
bb: string;
constructor create; overload;
procedure doit; override;
end;
classB = class of C_B;

procesBASE = procedure (any: C_BASE);

var
Form1: TForm1;

implementation

{$R *.dfm}

constructor C_A.create;
begin
Self.aa := 'iamaa';
Self.ii := 555;
Self.a0 := 'nbads';
end;

procedure C_A.doit;
begin
ShowMessage(Self.aa);
ShowMessage(Self.a0);
end;

constructor C_B.create;
begin
self.bb := 'bbii';
end;

procedure C_B.doit;
begin
ShowMessage(self.bb);
end;

procedure doany(var any: C_BASE; cls: classC);
var
innerbase: C_BASE;
begin
innerbase := any.create;
ShowMessage(innerbase.ClassName);
//<<<<<<<<<<<<<<<<<<<<<<<怎样在这里产生一个C_BASE子类实例啊?????
//cls.InstanceSize
any.doit;
end;

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

procedure TForm1.Button1Click(Sender: TObject);
var
ca: C_A;
cb: C_BASE;
cla: classA;
clb: classB;
clc: classC;
begin
ca := C_A.create;
clc := clb;
doany(C_BASE(ca), clc);
ca.Free;
// cb := C_BASE.create;
// doany(cb, clb);
// cb.Free;
//
// ca := cb;
end;

end.
...全文
184 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
applebomb 2006-09-22
  • 打赏
  • 举报
回复
抽象虚函数是一个错,关键的地方还是按照 小虫) (
做得正确:

TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
list: TList;
{ Private declarations }
public
{ Public declarations }
end;

C_BASE = class(TObject)
public
procedure doit; virtual; abstract;
end;
classC = class of C_BASE;

C_A = class(C_BASE)
private
ii: Integer;
public
aa: string;
a0: string;
constructor create;
procedure doit; override;
end;
classA = class of C_A;

C_B = class(C_BASE)
public
bb: string;
constructor create;
procedure doit; override;
end;
classB = class of C_B;

procesBASE = procedure (any: C_BASE);

var
Form1: TForm1;

implementation

{$R *.dfm}

constructor C_A.create;
begin
Self.aa := 'iamaa';
Self.ii := 555;
Self.a0 := 'nbads';
end;

procedure C_A.doit;
begin
ShowMessage(Self.aa);
ShowMessage(Self.a0);
end;

constructor C_B.create;
begin
self.bb := 'bbii';
end;

procedure C_B.doit;
begin
ShowMessage(self.bb);
end;

procedure doany(var any: C_BASE; cls: classC);
var
innerbase: C_BASE;
begin
innerbase := C_BASE(any.NewInstance);
ShowMessage(innerbase.ClassName);
//<<<<<<<<<<<<<<<<<<<<<<<怎样在这里产生一个C_BASE子类实例啊?????
//cls.InstanceSize
innerbase.doit;
end;

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

procedure TForm1.Button1Click(Sender: TObject);
var
ca: C_A;
cb: C_BASE;
cla: classA;
clb: classB;
clc: classC;
begin
ca := C_A.create;
clc := clb;
doany(C_BASE(ca), clc);
ca.Free;
// cb := C_BASE.create;
// doany(cb, clb);
// cb.Free;
//
// ca := cb;
end;
dabaicai 2006-09-21
  • 打赏
  • 举报
回复
至于你不要写构造的话,里面只要用inherited即可,默认也是
dabaicai 2006-09-21
  • 打赏
  • 举报
回复
构造函数不能为抽象虚函数,把abstract去掉
halfdream 2006-09-21
  • 打赏
  • 举报
回复
楼主你让建构函数也成了虚函数了..把这个改过来就行了.
具体原因,楼主可以看李维写的INSIDE VCL,

楼主应该有较好的JAVA或C++基础,仍建议在学DELPHI的时候,平心静气.可以说,DELPHI本身只是一种工具而已,真正会让您得益的,是它背后的风格,文化,思路等潜在的东西.

--------------------------------------------------------

C_BASE = class(TObject)
public
constructor create; //virtual; abstract;
procedure doit; virtual; abstract;
end;

classC = class of C_BASE;

C_A = class(C_BASE)
private
ii: Integer;
public
aa: string;
a0: string;
constructor create; //overload;
procedure doit; override;
end;
classA = class of C_A;

C_B = class(C_BASE)
public
bb: string;
constructor create; //overload;
procedure doit; override;
end;
classB = class of C_B;

procesBASE = procedure (any: C_BASE);

var
Form1: TForm1;

implementation

{$R *.dfm}

constructor C_A.create;
begin
aa := 'iamaa';
ii := 555;
a0 := 'nbads';
end;

procedure C_A.doit;
begin
ShowMessage(Self.aa);
ShowMessage(Self.a0);
end;

constructor C_B.create;
begin
bb := 'bbii';
end;

procedure C_B.doit;
begin
ShowMessage(self.bb);
end;

procedure doany(var any: C_BASE; cls: classC);
var
innerbase: C_BASE;
begin
innerbase := cls.create;
ShowMessage(innerbase.ClassName);
//cls.InstanceSize
any.doit;
end;

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

procedure TForm1.Button1Click(Sender: TObject);
var
ca: C_A;
cb: C_BASE;
cla: classA;
clb: classB;
clc: classC;
begin
ca := C_A.create;
// clc := clb;
// clc := classB;

doany(C_BASE(ca), C_B);

ca.Free;

// cb := C_BASE.create;
// doany(cb, clb);
// cb.Free;
//
// ca := cb;
end;
{ C_BASE }

constructor C_BASE.create;
begin
//
end;
sdzbbz 2006-09-21
  • 打赏
  • 举报
回复
GARNETT2183 2006-09-21
  • 打赏
  • 举报
回复
类类型就可以做到了...
postren 2006-09-21
  • 打赏
  • 举报
回复
这样呢?

procedure doany(var any: C_BASE; cls: classC);
var
innerbase: C_BASE;
begin
innerbase := C_BASE(any.NewInstance);
ShowMessage(innerbase.ClassName);
//<<<<<<<<<<<<<<<<<<<<<<<怎样在这里产生一个C_BASE子类实例啊?????
//cls.InstanceSize
any.doit;
end;

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧