为什么会出现类型不兼容的错误?(100分)

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

mycwcgr_temp

Unregistered / Unconfirmed
GUEST, unregistred user!
为什么会出现类型不兼容的错误?
tfindfile是一个类。
tfindfile.create(aowner:TComponent);是它的构造函数
为什么 myfindfile:=tfindfile.Create(self)会出错 ([Error] Unit1.pas(43): Incompatible types: 'TComponent' and 'tSearchAndDelete' )
而myfindfile:=tfindfile.Create(form1)就没有错误?
要知道tSearchAndDelete = class(TObject) 是从 Tobject 继承的是 TComponent的祖先类

//---------------------------------------------------------------------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
FindFile;
type
tSearchAndDelete = class(TObject)
path:string;
list:tstringlist;
myfindfile:tfindfile;
private
procedure newFindFile1Found(Sender: TObject;
Folder: String;
var FileInfo: TSearchRec);
public
constructor create(inputpath:string);
destructor destroy;override;
end;

type
TForm1 = class(TForm)
FindFile1: TFindFile;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
{ tSearchAndDelete }
constructor tSearchAndDelete.create(inputpath:string);
begin
list:=tstringlist.Create;
myfindfile:=tfindfile.Create(self);
//此语句出错++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
end;

destructor tSearchAndDelete.destroy;
begin
list.Free;
myfindfile.free;
inherited destroy;
end;

procedure tSearchAndDelete.newFindFile1Found(Sender: TObject;
Folder: String;
var FileInfo: TSearchRec);
begin
//
end;

end.
 
myfindfile:=tfindfile.Create(form1) //父亲是form1
myfindfile:=tfindfile.Create(self);
//父亲是自己 (本类:tSearchAndDelete)
在这里self可不是form1, 够清楚了吧

 
对啊!self是tSearchAndDelete,而它Tobject的子类啊?
难道要用 myfindfile:=tfindfile.Create(tobject(self));
或者是myfindfile:=tfindfile.Create(self as tobject);

 
myfindfile:=tfindfile.Create(tobject(self));
或者是myfindfile:=tfindfile.Create(self as tobject);

仍然出现同样错误
 
myfindfile:=tfindfile.Create(nil);不会出错,我想问一问是为什么?
 
stlont说的对!
SearchAndDelet没本事当父亲!
 
SearchAndDelet为什么不能作父类
 
因为它没能力,哈哈
 
-->SearchAndDelet为什么不能作父类
SearchAndDelet可以作父类,但不是在这里
constructor本来就是要生出SearchAndDelet来
可是,看我的注释 //父亲是自己 (本类:tSearchAndDelete)

 
"SearchAndDelet可以作父类,但不是在这里
constructor本来就是要生出SearchAndDelet来"
我将 myfindfile:=tfindfile.Create(self as tobject);
放在 procedure aa;过程
中仍然出现同样错误
//======================================
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
FindFile;
type
tSearchAndDelete = class(TObject)
path:string;
list:tstringlist;
myfindfile:tfindfile;
private
procedure newFindFile1Found(Sender: TObject;
Folder: String;
var FileInfo: TSearchRec);
public
constructor create(inputpath:string);
procedure aa;
destructor destroy;override;
end;

。。。。
procedure tSearchAndDelete.aa;
begin
myfindfile:=tfindfile.Create(self as tobject);
//此语句出错++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
end;
 
type
tSearchAndDelete = class(TObject) //这里改成class(TComponent)就可以了, 然后
//需要修改构造过程的Create
或者
procedure tSearchAndDelete.aa;
begin
myfindfile:=tfindfile.Create(nil);
//此语句出错++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
end;
 
type
tSearchAndDelete = class(TObject) //这里改成class(TComponent)就可以了, 然后
//需要修改构造过程的Create
改为TComponent不行
 
[?]怎么不行?
 
([Error] Unit1.pas(43): Incompatible types: 'TComponent' and 'tSearchAndDelete' )
出现同样错误!
反推一下Tobject 是 Tcompint的交类,Tcompint可以的话,Tobject也一定可以
 
???????
myfindfile:=tfindfile.Create(TComponent(self));
 
myfindfile:=tfindfile.Create(nil);
 
看一下Create方法。那个参数是TComponet,只兼容TComponet或TComponet的子类
当然不能和TOBject兼容了。
但是TObject可以向下兼容。
如果参数类型是TObject则可以用TObject的一切子类
 
很奇怪myfindfile:=tfindfile.Create(self as TComponent )不行
而myfindfile:=tfindfile.Create(TComponent(self));可以
难道两个语句不等效吗?
 
myfindfile:=tfindfile.Create(self as TComponent);//类型不匹配会报错
myfindfile:=tfindfile.Create(TComponent(self));//强制转换
 
self as TComponent转换前先进行了检查,比较保险,
也是目前标准的写法,
TComponent(self)不进行检查,你要对你的行为负全部责任。
 
顶部