关于接口(100分)

  • 主题发起人 完颜康
  • 开始时间

完颜康

Unregistered / Unconfirmed
GUEST, unregistred user!
Type
IMyInterface = interface
Procedure P1;
procedure P2;
end;
Type
TMyImplClass = Class
Procedure P1;
procedure P2;
end;
Type
TMyClass = (TInterfaceObject,IMyInterFace)
FMyImplClass : TMyImplClass;
property MyImplClass :TMyImplClass read FMyImplClass implements IMyInterface
procedure ImyInterface.p1= MyP1
Procedure MYp1;
end;
var Myclass :TMyClass
MyInterface :ImyInterface ;
begin
MyClass := TMyClass.create;
MyClass.FMyImplclass := TMyImplClass.Create;
MyInterface.P1;//1
MyInterface.P2;//2
请问1、2处分别调用哪个过程?为什么这么调用?
也可以关于这个主题随便说说...
 
推荐一本书《Delphi COM深入编程》
书很不错,翻译一塌糊涂。
 
>>翻译一塌糊涂
那我是买还是不买呢
 
一本书从头到尾只要有一句话对自己有用,它就值得买。
 
你的问题可参考该书1.5节。
 
你的程序漏洞百出! 修改如下
///////////////////////////////////////////////////////////////////
type
IMyInterface = interface
procedure p1;
procedure p2;
end;

TMyImplClass = class(TInterfacedObject, IMyInterface)
procedure p1;
procedure p2;
end;

TMyClass = class(TInterfacedObject, IMyInterFace)
FMyImplClass: TMyImplClass;
property MyImplClass: TMyImplClass read FMyImplClass implements IMyInterface;
procedure IMyInterFace.p1 = MyP1;
procedure MYp1;
end;

proceduredo
It;
implementation
proceduredo
It;
var
Myclass: TMyClass;
MyInterface: ImyInterface;
begin
MyClass := TMyClass.create;
MyClass.FMyImplclass := TMyImplClass.Create;
MyInterface:= Myclass;
MyInterface.P1;
//1 这里调用的是TMyClass的方法
MyInterface.P2;
//2 这里调用的是TMyImplClass的方法
end;

{ TMyImpleClass }
procedure TMyImplClass.p1;
begin
showmessage('TMyImplClass.p1');
end;

procedure TMyImplClass.p2;
begin
showmessage('TMyImplClass.p2');
end;

{ TMyClass }
procedure TMyClass.MYp1;
begin
showmessage('TMyClass.MYp1');
end;
end.
///////////////////////////////////////////////////////////////////
 
楼上的大侠辛苦了~要是再有写说明性的文字来将为什么这么调用就好了
 
savenight你的怎么有两个实现类??
我的理解是这样的:
type
定义一个纯虚的接口,如果你的App是DLL,那么在其它的编绎器中可以按个接口
重新定义这样一个接口,像我们用Import OCX/DLL 产生出来的单元一样,那么一大堆的接口,类型,函数,
它都是Delphi的语法,但是只有调用。
IMyInterface = interface
procedure p1;
procedure p2;
end;

实现类,对接口的实现,TInterfaceObject是一个为接口实现了_addref, _release, queryinterface的类
所以我们写对某个接口的时候,可以不写它了,不然的话我们写接口是这样:
{
TMyImpClass = class(TObject, ImyInterface)
protected
// IUnknown
function _AddRef: Integer;
function _Release: Integer;
function QueryInterface(...)://参数忘了。
// ImyInterface
procedure P1;
procedure P2
end;
}
否则的话编绎时会有说_addref,_release, queryinterface没有实现之类的话
TMyImplClass = class(TInterfacedObject, IMyInterface)
private
//other variant, func
protected
//IMyInterface实现过程,函数
procedure p1;
procedure p2;
public
//other func
end;

//delphi封装这个接口所定义类。用来实现这个接口
TMyClass = class
class function Create: IMyInterface;
class function CreateRemote(RemoteName: string);
end;

class function TmYClass.Create: IMyInterface;
begin
Result := Comobj.CreateComObject(MyInterfaceIID) as IMyInterface;
end;

class function TMyClass.CreateRemote(RemoteName: string);
begin
Result := Comobj.CreateRemoteComObject(MyInterfaceIID) as IMyInterface;
end;

写MIDAS中,我们常常是做写数据模块,看见的差不多就是这样的样子。
定义接口,方法,函数,属性,我们在RemoteDataModule写的实现代码,TRemoteDataModule
就是这个接口的实现类,它那有你在接口的函数,过程,属性的具体实现代码。
不知理解有误没?
 
其实我的代码写得很详细了。
MyInterface.P1;
//1
这里为什么调用的是TMyClass的方法?是因为这一句procedure IMyInterFace.p1 = MyP1;把
原来是由TMyImplClass做的工作委托给了TMyClass的MyP1方法,如果没有procedure IMyInterFace.p1 = MyP1;
那么1处的调用就是原来的TMyImplClass.p1.
MyInterface.P2;
//2
这里为什么调用的是TMyImplClass的方法?我想不用我说了吧,就是因为没有让人代劳,还是由
TMyImplClass自己来做。
 
用interface有一点一定要注意,就是不要自己释放,切忌
 
我来学!
 
今天看一本Delphi3的COM一些介绍,它说在这样的情况下用接口不太好:
procedure SomeProc(AInterface: IMyInterface);
begin
AInterface.DoSomething
end;

procedure CallInterface;
begin
SomeProc(myInterface);
//在这里调用完后MyInterface给另外的过程引用后,好像没有_Addref,但是却_release
//到这里后MyInterface将会Destroy,是不是会这样??
end;
 
savenight
还有像你写,是不是说TMyClass没有对p2方法进行实现,它不会出错吧?(我这是网吧)
那它调用p2,p2会自动去找其它实现接口的类?难道它p2会自动创建TMyImpClass的实例,
然后又destroy???
 
接受答案了.
 
顶部