关于类引用的问题,我做了个例子 但不对,帮我看看(100分)

  • 主题发起人 主题发起人 wyismail
  • 开始时间 开始时间
W

wyismail

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

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

type
TObj = class //TObject是一个类,而且是最基本的类。
end;
TClass = class of TObj;// TClass是类定义。

TA = class
end;
TAclass = class of TA;

TB = class
end;
TBclass = class of TB;


TForm1 = class(TForm)
private

{ Private declarations }
public
function MyCreateObject(C:TClass): TObject;
procedure Test;
{ Public declarations }
end;





var
Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.MyCreateObject(C: TClass): TObject;
begin
Result := C.Create;
end;

procedure TForm1.Test;
var O: TObject;
begin
O:= MyCreateObject(TAclass);
ShowMessage(O.ClassName);
O.Free;
O:= MyCreateObject(TBclass);
ShowMessage(O.ClassName);
O.Free;

end;

end.


错误:
[Error] Unit1.pas(52): '(' expected but ')' found
[Error] Unit1.pas(53): Incompatible types: 'TClass' and 'TAclass'
[Error] Unit1.pas(55): '(' expected but ')' found
[Error] Unit1.pas(56): Incompatible types: 'TClass' and 'TBclass'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
 
類裡沒有方法、事件、屬性,不能這樣調用。
先加入方法、事件、屬性然後就可以用了。
 
导致以下这两个问题的原因可能是你的TA,TB不是从TObj继承来的,而是和TObj一样从TObject继承的:
[Error] Unit1.pas(53): Incompatible types: 'TClass' and 'TAclass'
[Error] Unit1.pas(56): Incompatible types: 'TClass' and 'TBclass'
 
function TForm1.MyCreateObject(C: TClass): TObject;
C是TClass的实例,但是你的语句中TAclass却不是TClass的实例.
MyCreateObject(TAclass);
 
[:(]还是不知道问题
 
后退
顶部