16,748
社区成员
发帖
与我相关
我的任务
分享
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;
IFormattedNumber = interface
function FormattedString : String;
end;
TFormattedInteger = class(TObject,IFormattedNumber)
private
FValue : Integer;
FRefCount : Integer;//引用计数
public
constructor Create(AValue : Integer);
//IUnknown 接口
function QueryInterface(const IID : TGUID;out Obj): HResult; stdcall;
function _AddRef : Integer; Stdcall;
function _Release : Integer; stdcall;
//IFormattedNumber 接口
function FormattedString : String;
procedure SetValue(AValue : Integer);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
constructor TFormattedInteger.Create(AValue : Integer);
begin
inherited Create;
FValue := AValue;
end;
function TFormattedInteger.QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
const
E_NOINTERFACE = $80004002;
begin
if GetInterface(IID,Obj) then
Result := 0
else
Result := E_NOINTERFACE;
end;
function TFormattedInteger._AddRef: Integer;stdcall;
begin
Inc(FRefCount);
Result := FRefCount;
end;
function TFormattedInteger._Release: Integer; stdcall;
begin
Dec(FRefCount);
Result := FRefCount;
If FRefCount = 0 then
Destroy;
end;
//IFormattedNumber接口
function TFormattedInteger.FormattedString : String;
begin
Result := 'The value is' + IntToStr(FValue);
end;
procedure TFormattedInteger.SetValue(AValue : Integer);
begin
FValue := AValue;
end;
end.
function TInterfacedObject._AddRef: Integer;
begin
Result := InterlockedIncrement(FRefCount);
end;
function TInterfacedObject._AddRef: Integer;
begin
Result := InterlockedIncrement(FRefCount);
end;