unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
tBase = class(TPersistent)
public
procedure virFunc; virtual;
procedure SomeFunc;
end;
PTBase = ^TBase; tTest = class(tBase)
public
procedure virFunc; override;
procedure SomeFunc;
end;
PTtest = ^TTest; TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
list1: TList;
list2: TList; procedure funcA;
procedure funcB;
end;var
Form1: TForm1;implementation{$R *.dfm}procedure tBase.virFunc;
begin
ShowMessage('base_virFunc');
end;procedure tTest.virFunc;
begin
ShowMessage('test_virFunc');
end;procedure TForm1.funcA;
var
t : TTest;
test : TTest;
pTest : PTBase;
begin
//往list及list1中分别加入一个对象和对象的指针
t := TTest.Create;
list1.Add(t);
list2.Add(@t); //分别取出对象及对象的指针,并调用对象的方法SomeFunc
test := TTest(list1.items[0]);
pTest := PTBase(list2.items[0]);
test.SomeFunc;
pTest.SomeFunc;end;procedure TForm1.funcB;
var
test : TTest;
pTest : PTBase;
begin
//分别取出对象及对象的指针,并调用对象的方法SomeFunc
test := TTest(list1.items[0]);
pTest := PTBase(list2.items[0]);
test.SomeFunc;
pTest.SomeFunc; //出错
end;procedure TForm1.FormCreate(Sender: TObject);
begin
list1 := TList.Create;
List2 := TList.Create;
funcA;
funcB;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
List1.Free;
List2.Free;
end;{ tTest }procedure tBase.SomeFunc;
begin
ShowMessage('base');
end;procedure tTest.SomeFunc;
begin
ShowMessage('test');
end;end.
上面的程序,如果把TBase中的SomeFunc改为虚函数就会出错, 为什么?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
tBase = class(TPersistent)
public
procedure virFunc; virtual;
procedure SomeFunc;
end;
PTBase = ^TBase; tTest = class(tBase)
public
procedure virFunc; override;
procedure SomeFunc;
end;
PTtest = ^TTest; TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
list1: TList;
list2: TList; procedure funcA;
procedure funcB;
end;var
Form1: TForm1;implementation{$R *.dfm}procedure tBase.virFunc;
begin
ShowMessage('base_virFunc');
end;procedure tTest.virFunc;
begin
ShowMessage('test_virFunc');
end;procedure TForm1.funcA;
var
t : TTest;
test : TTest;
pTest : PTBase;
begin
//往list及list1中分别加入一个对象和对象的指针
t := TTest.Create;
list1.Add(t);
list2.Add(@t); //分别取出对象及对象的指针,并调用对象的方法SomeFunc
test := TTest(list1.items[0]);
pTest := PTBase(list2.items[0]);
test.SomeFunc;
pTest.SomeFunc;end;procedure TForm1.funcB;
var
test : TTest;
pTest : PTBase;
begin
//分别取出对象及对象的指针,并调用对象的方法SomeFunc
test := TTest(list1.items[0]);
pTest := PTBase(list2.items[0]);
test.SomeFunc;
pTest.SomeFunc; //出错
end;procedure TForm1.FormCreate(Sender: TObject);
begin
list1 := TList.Create;
List2 := TList.Create;
funcA;
funcB;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
List1.Free;
List2.Free;
end;{ tTest }procedure tBase.SomeFunc;
begin
ShowMessage('base');
end;procedure tTest.SomeFunc;
begin
ShowMessage('test');
end;end.
上面的程序,如果把TBase中的SomeFunc改为虚函数就会出错, 为什么?
解决方案 »
- 编译工程时出现错误“[Error] File not found: 'DIALOG.RES'”如何解决?
- ClientSocket和ServerSocket之间交互通信的流程图怎么画更详细、完整些呢?
- 求助,请教 SQL SERVER 2000 备份的一个小问题,谢谢 !
- 通过DELPHI把两个关联表的数据导出到文本文件里去,想了一天,晕呀??
- 如何动态改变Windows右下角程序图标?
- 用terminate关闭TThread对象时和操作系统的版本有关系吗?
- Fieldbyname('XXX').asstring会trim掉右边的空格!
- DataModule上不能放ADO控件吗?奇了。
- 报表的打印与预览
- 请看如下问题,搞得我睡不着
- 再来求各位了,在线送分。。。
- 怎样用DELPHI启动WIN98的拨号网络服务器并将某一MODEM设为允许拨入的状态?
procedure tTest.virFunc;
begin
ShowMessage('test_virFunc');
end;这一段,