在学Com时遇到问题implements,难以理解,看了帮助,有点迷迷糊糊,请示教!(100分)

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

maikee1978

Unregistered / Unconfirmed
GUEST, unregistred user!
英文看了,不甚理解.请用中文解说一下,特别是其中的事例,不胜感激
**********************************************************************
The implements directive allows you to delegate implementation of an interface to a property in the implementing class. For example,

property MyInterface: IMyInterface read FMyInterface implements IMyInterface;

declares a property called MyInterface that implements the interface IMyInterface.
The implements directive must be the last specifier in the property declaration and can list more than one interface, separated by commas. The delegate property

must be of a class or interface type.
cannot be an array property or have an index specifier.
must have a read specifier. If the property uses a read method, that method must use the default register calling convention and cannot be dynamic (though it can be virtual) or specify the message directive.

Note: The class you use to implement the delegated interface should derive from TAggregatedObject.

*************************************************************************
If the delegate property is of an interface type, that interface, or an interface from which it derives, must occur in the ancestor list of the class where the property is declared. The delegate property must return an object whose class completely implements the interface specified by the implements directive, and which does so without method resolution clauses. For example,

type

IMyInterface = interface
procedure P1;
procedure P2;
end;

TMyClass = class(TObject, IMyInterface)

FMyInterface: IMyInterface;
property MyInterface: IMyInterface read FMyInterface implements IMyInterface;
end;

var

MyClass: TMyClass;
MyInterface: IMyInterface;
begin
MyClass := TMyClass.Create;
MyClass.FMyInterface := ... // some object whose class implements IMyInterface
MyInterface := MyClass;
MyInterface.P1;
end;

****************************************************************************
If the delegate property is of a class type, that class and its ancestors are searched for methods implementing the specified interface before the enclosing class and its ancestors are searched. Thus it is possible to implement some methods in the class specified by the property, and others in the class where the property is declared. Method resolution clauses can be used in the usual way to resolve ambiguities or specify a particular method. An interface cannot be implemented by more than one class-type property. For example,

type

IMyInterface = interface
procedure P1;
procedure P2;
end;
TMyImplClass = class
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;
procedure TMyImplClass.P1;
...
procedure TMyImplClass.P2;
...
procedure TMyClass.MyP1;
...
var
MyClass: TMyClass;
MyInterface: IMyInterface;
begin
MyClass := TMyClass.Create;
MyClass.FMyImplClass := TMyImplClass.Create;
MyInterface := MyClass;
MyInterface.P1; // calls TMyClass.MyP1;
MyInterface.P2; // calls TImplClass.P2;
end;
 
去买本中文版的吧!
 
都摸索着快看完了,单词都明白.是对所说的意义不太理解.
这个是帮助你的资料,有中文帮助???
 
好像是说,一个委托的属性,是一个接口实例或者一个完全实现了此接口的对象
implements IMyInterface
表示 后面的那个接口是由此属性对应的接口或对象实现的,
用来实现COM对象的包容
 
此属性本身就是用 IMyinterface声明的类型,而此类型又应是Automate_LIB中定义的一个接口,此属性就已经对应了IMyInterface接口,再委托给IMyInterface?怎么好象转圈的感觉.
 
这个不是COM的问题,是一个完整的接口问题。
这段代码是,接口的实现将会引用到另一个对象中对该接口的实现。上面代码中,FMyInterface将会使用另一个对象。
 
这个对象如何确定呢?如下面的例子中
unit ServAuto;

interface

{$WARN SYMBOL_PLATFORM OFF}

uses
ComObj, ActiveX, AxCtrls, Server_TLB, StdVcl;

type
TServerWithEvents = class(TAutoObject, IConnectionPointContainer, IServerWithEvents)
private
{ Private declarations }
FConnectionPoints: TConnectionPoints;
FEvents: IServerWithEventsEvents;
procedure MemoChange(Sender: TObject);
public
procedure Initialize; override;
protected
{ Protected declarations }
property ConnectionPoints: TConnectionPoints read FConnectionPoints
implements IConnectionPointContainer;//TConnectionPoints是在哪个对象中实现???

procedure EventSinkChanged(const EventSink: IUnknown); override;
procedure Clear; safecall;
procedure AddText(const NewText: WideString); safecall;
end;

implementation

uses ComServ, ServMain, SysUtils, StdCtrls;

procedure TServerWithEvents.EventSinkChanged(const EventSink: IUnknown);
begin
FEvents := EventSink as IServerWithEventsEvents;
end;

procedure TServerWithEvents.Initialize;
begin
inherited Initialize;
FConnectionPoints := TConnectionPoints.Create(Self);
if AutoFactory.EventTypeInfo <> nil then
FConnectionPoints.CreateConnectionPoint(AutoFactory.EventIID,
ckSingle, EventConnect);
// Route main form memo's OnChange event to MemoChange method:
MainForm.Memo.OnChange := MemoChange;
end;

procedure TServerWithEvents.Clear;
begin
MainForm.Memo.Lines.Clear;
if FEvents <> nil then FEvents.OnClear;
end;

procedure TServerWithEvents.AddText(const NewText: WideString);
begin
MainForm.Memo.Lines.Add(NewText);
end;

procedure TServerWithEvents.MemoChange(Sender: TObject);
begin
if FEvents <> nil then FEvents.OnTextChanged((Sender as TMemo).Text);
end;

initialization
TAutoObjectFactory.Create(ComServer, TServerWithEvents,
Class_ServerWithEvents, ciMultiInstance, tmApartment);
end.
 
TConnectionPoints是一个类啊,这个类实现了你的IConnectionPointContainer,但是未必支持你的IConnectionPointContainer接口.呵呵!
不过你的IConnectionPointContainer中定义的方法,在类TConnectionPoints一定能找到对应的方法
 
是,这是关于一个类的引用
原英文的是帮助里面的两个例子,分别是委派属性是接口和类类型.
我想他们的原理应是一样的吧.
If the delegate property is of an interface type,....
If the delegate property is of a class type,....
 
没得答案,结贴了事,唉!
 

Similar threads

I
回复
0
查看
1K
import
I
I
回复
0
查看
2K
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
948
import
I
后退
顶部