接口的问题

panxiaosen 2009-03-02 11:43:54
初学接口,又碰到一个问题

unit Unit2;

interface
uses
Windows;

type
IBase = interface
procedure a;
end;

IMyTest = interface(IBase)
procedure b;
end;

tbase = class(TInterfacedObject, ibase)
procedure a;
end;

tmytest = class(tbase, imytest)
procedure b;
end;

function CreateMytest: IBase;
implementation

{ tbase }

procedure tbase.a;
begin

end;

{ tmytest }

procedure tmytest.b;
begin
MessageBox(0, 'test', 'test', MB_OK);
end;

function CreateMytest: IBase;
begin
Result := tmytest.create;
end;
end.



为什么
a := CreateMytest;
imytest(a).b;
不行


但是

a := CreateMytest;
Tmytest(a).b;
就可以了呢?


我是准备通过接口来操作类的...应该怎么改好呢??
...全文
153 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
panxiaosen 2009-03-03
  • 打赏
  • 举报
回复
呵呵 结贴了,现在可以用了,就不知道有没问题...... 接口,COM,我是一窍不通..继续学习中...可能还要叨扰各位 :)
qiume 2009-03-03
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 panxiaosen 的回复:]
引用 3 楼 3150379 的回复:
Delphi(Pascal) code
var a: IBase;
begin
a:=tbase.Create;
//或者a:=tmytest.Create;
tmytest(a).b;
end;


这样就可以通过接口来操作类


tmytest(a).b;

这样子不是还要引用类吗? 我想把类封装到DLL里,只提供接口来操作.. 难道没办法了吗?
[/Quote]

如果要传递接口,应该使用COM对象而不是DLL......
starluck 2009-03-03
  • 打赏
  • 举报
回复

兄弟你再看下你的代碼,你確定你的理論?
bdmh 2009-03-03
  • 打赏
  • 举报
回复
delphi帮助中的解释
Classes that implement interfaces can use the as operator for dynamic binding on the interface
panxiaosen 2009-03-03
  • 打赏
  • 举报
回复
刚才试了一下

imytest(a).b;

改成
(a as imytest).b;

就可以了?? 哪位兄弟来解释一下? 还有这样使用会不会有问题?
panxiaosen 2009-03-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 starluck 的回复:]


// 測試:
procedure TForm2.Button1Click(Sender: TObject);
var
a : IMyTest;
begin
a := CreateMytest;
IMyTest(A).b;
end;


[/Quote]


a不能定义成具体的派生接口,因为会有很多个从IBASE派生出来的接口,CreateMytest返回哪个派生接口是未知的,所以只能定义成IBASE
starluck 2009-03-03
  • 打赏
  • 举报
回复


unit Unit3;

interface
uses
Windows;

type
IBase = interface
procedure a;
end;

IMyTest = interface(IBase)
procedure b;
end;

Tbase = class(TInterfacedObject, ibase)
procedure a;
end;

Tmytest = class(tbase, imytest)
procedure b;
end;

function CreateMytest: imytest;
implementation

{ tbase }

procedure tbase.a;
begin

end;

{ tmytest }

procedure tmytest.b;
begin
MessageBox(0, 'test', 'test', MB_OK);
end;

function CreateMytest: imytest;
begin
Result := Tmytest.create;
end;
end.





// 測試:
procedure TForm2.Button1Click(Sender: TObject);
var
a : IMyTest;
begin
a := CreateMytest;
IMyTest(A).b;
end;

7年 2009-03-03
  • 打赏
  • 举报
回复
学习,帮顶。
panxiaosen 2009-03-03
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 3150379 的回复:]
Delphi(Pascal) code
var a: IBase;
begin
a:=tbase.Create;
//或者a:=tmytest.Create;
tmytest(a).b;
end;



这样就可以通过接口来操作类
[/Quote]

tmytest(a).b;

这样子不是还要引用类吗? 我想把类封装到DLL里,只提供接口来操作.. 难道没办法了吗?
五维思考 2009-03-03
  • 打赏
  • 举报
回复

var a: IBase;
begin
a:=tbase.Create;
//或者a:=tmytest.Create;
tmytest(a).b;
end;

这样就可以通过接口来操作类
五维思考 2009-03-03
  • 打赏
  • 举报
回复
因为Tmytest实现了IBase和IMyTest接口,所以Tmytest(a).b这样是正常的

IBase和IMyTest接口没有关系,所以imytest(a).b转换肯定会出错的
panxiaosen 2009-03-02
  • 打赏
  • 举报
回复
对了,a的声明如下

a: IBase;

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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