混合应用接口和对象模型的意外
2006-03-28 22:29
441 查看
按照Eric Harmon 的说法,如下所示
IFormattedNumber = interface
['{86EF89E2-347C-480F-9A6C-1E57F134E58E}']
function FormatttedString: string;
procedure SetValue(AValue: Integer);
end;
TFormattedInteger = class(TObject, IFormattedNumber)
private
FRefCount: Integer;
FValue: Integer;
public
constructor Create(aValue: Integer);
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
function FormatttedString: string;
procedure SetValue(AValue: Integer);
end;
procedure DoSomethingWithInterface(Intf: IFormattedNumber);
implementation
procedure DoSomethingWithInterface(Intf: IFormattedNumber);
begin
ShowMessage(Intf.FormatttedString);
end;
procedure CreateAndUseObject;
var
MyInteger: TFormattedInteger;
begin
MyInteger := TFormattedInteger.Create(12);
DoSomethingWithInterface(MyInteger as IFormattedNumber);
MyInteger.SetValue(10);
end;
{ TFormattedInteger }
constructor TFormattedInteger.Create(aValue: Integer);
begin
inherited Create;
FValue := aValue;
end;
function TFormattedInteger.FormatttedString: string;
begin
Result := 'The Value is ' + IntToStr(FValue)
end;
function TFormattedInteger._AddRef: Integer;
begin
Inc(FRefCount);
Result := FRefCount;
end;
function TFormattedInteger._Release: Integer;
begin
Dec(FRefCount);
Result := FRefCount;
if FRefCount = 0 then
Destroy;
end;
function TFormattedInteger.QueryInterface(const IID: TGUID;
out Obj): HRESULT;
const
E_NOINTERFACE = $80004002;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
procedure TFormattedInteger.SetValue(AValue: Integer);
begin
FValue := AValue;
end;
结果是编译也通不过:
[Error] Unit1.pas(52): Operator not applicable to this operand type
这句有问题了:
DoSomethingWithInterface(MyInteger as IFormattedNumber);
当然这样是没有问题的:DoSomethingWithInterface(MyInteger as IFormattedNumber);
真奇怪,本来是来验证as 对 引用计数的影响的,结果进行不下去了。
IFormattedNumber = interface
['{86EF89E2-347C-480F-9A6C-1E57F134E58E}']
function FormatttedString: string;
procedure SetValue(AValue: Integer);
end;
TFormattedInteger = class(TObject, IFormattedNumber)
private
FRefCount: Integer;
FValue: Integer;
public
constructor Create(aValue: Integer);
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
function FormatttedString: string;
procedure SetValue(AValue: Integer);
end;
procedure DoSomethingWithInterface(Intf: IFormattedNumber);
implementation
procedure DoSomethingWithInterface(Intf: IFormattedNumber);
begin
ShowMessage(Intf.FormatttedString);
end;
procedure CreateAndUseObject;
var
MyInteger: TFormattedInteger;
begin
MyInteger := TFormattedInteger.Create(12);
DoSomethingWithInterface(MyInteger as IFormattedNumber);
MyInteger.SetValue(10);
end;
{ TFormattedInteger }
constructor TFormattedInteger.Create(aValue: Integer);
begin
inherited Create;
FValue := aValue;
end;
function TFormattedInteger.FormatttedString: string;
begin
Result := 'The Value is ' + IntToStr(FValue)
end;
function TFormattedInteger._AddRef: Integer;
begin
Inc(FRefCount);
Result := FRefCount;
end;
function TFormattedInteger._Release: Integer;
begin
Dec(FRefCount);
Result := FRefCount;
if FRefCount = 0 then
Destroy;
end;
function TFormattedInteger.QueryInterface(const IID: TGUID;
out Obj): HRESULT;
const
E_NOINTERFACE = $80004002;
begin
if GetInterface(IID, Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
procedure TFormattedInteger.SetValue(AValue: Integer);
begin
FValue := AValue;
end;
结果是编译也通不过:
[Error] Unit1.pas(52): Operator not applicable to this operand type
这句有问题了:
DoSomethingWithInterface(MyInteger as IFormattedNumber);
当然这样是没有问题的:DoSomethingWithInterface(MyInteger as IFormattedNumber);
真奇怪,本来是来验证as 对 引用计数的影响的,结果进行不下去了。
相关文章推荐
- Objective-C对象模型及应用
- 将对象直接转换为接口应用的代码例子, 注意其中用了 as 方式, 很有意思
- 面向对象高级续2(instanceof关键字、抽象类和接口的应用、Object类、包装类)
- JS应用DOM入门:和DHTML对象模型的比较
- Objective-C对象模型及应用
- 数据访问接口体系及数据对象模型探讨--[附录]
- 持久化模式,第 1 部分: 现代 ORM 工具的策略和最佳实践,使用 Hibernate 特性在领域模型上应用面向对象原则
- BOM对象模型应用分析
- 浏览器对象BOM模型开发示意图分析(应用设计)
- Matlab与C和C++混合编程接口及应用
- php学习笔记 面向对象中[接口]与[多态性]的应用
- Java面向对象高级--抽象类与接口的应用
- 第一天:Java源码级实战速成(通过动手实战类、对象等,通过Spark和Hadoop案例代码和源码解析具体指知识的应用、深度详解匿名接口在Spark开发中的运用)
- Objective-C对象模型及应用
- Objective-C对象模型及应用
- Objective-C对象模型及应用
- ACE主要应用框架与类别(七):ACE主动对象模型-Active Object
- DOM对象模型四大基本接口
- COM线程模型 - MTA接口 (传递MTA COM对象给STA套间线程)
- Crystal 三种报表解决方案大对比:.NET 对象模型,报表应用服务器对象模型,水晶企业对象模型