A
Archerfl
Unregistered / Unconfirmed
GUEST, unregistred user!
我的代码如下:
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
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TMyClass = class of TParent;
TParent = class
end;
TChild = class(TParent)
procedure ChildProc;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
k: TMyClass;
a: TParent;
begin
a := TParent.Create;
k := TChild;
end;
{ TChild }
procedure TChild.ChildProc;
begin
//
end;
end.
上面这段代码很简单,我在按钮的OnClick事件中声明了一个对象a,当我在编写代码时用
a.ChildProc肯定不行!因为a的类型为TParent,除非我进行类类型转换,这样调整后:
TChild(a).ChildProc没问题的!
但是,假如我不这样转换,而是先声明一个类引用类型变量k,然后将TChild赋值给它,之
后用k(a).ChildProc,这时我无法调用到该过程,编译器给我罗列出一堆类方法,请问这是
为什么呢?
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
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TMyClass = class of TParent;
TParent = class
end;
TChild = class(TParent)
procedure ChildProc;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
k: TMyClass;
a: TParent;
begin
a := TParent.Create;
k := TChild;
end;
{ TChild }
procedure TChild.ChildProc;
begin
//
end;
end.
上面这段代码很简单,我在按钮的OnClick事件中声明了一个对象a,当我在编写代码时用
a.ChildProc肯定不行!因为a的类型为TParent,除非我进行类类型转换,这样调整后:
TChild(a).ChildProc没问题的!
但是,假如我不这样转换,而是先声明一个类引用类型变量k,然后将TChild赋值给它,之
后用k(a).ChildProc,这时我无法调用到该过程,编译器给我罗列出一堆类方法,请问这是
为什么呢?