⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 Delphi面向对象编程思想附书源码 好用哦!
💻 PAS
字号:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

 type
 IGreetable=interface
   ['{FE5A34E5-21AB-4120-971B-FDC3241AD55D}']
   function SayHello:string;
 end;

 TMan=class(Tobject,IGreetable)
   function SayHello:string;
   function QueryInterface(const IID:TGUID; out Obj):HResult;stdcall;
   function _AddRef:Integer;stdcall;
   function _Release:Integer;stdcall;
 end;

 TForm1 = class(TForm)
   Button1: TButton;
   procedure Button1Click(Sender: TObject);
 private
   procedure Greeting(Intf:IGreetable);
   { Private declarations }
 public
   { Public declarations }
 end;

var
   Form1: TForm1;

implementation
{$R *.DFM}

// 这里实现自己定义的_AddRef、_Release和QueryInterface方法
function TMan.QueryInterface(const IID:TGUID;out Obj):HResult;
begin
  if GetInterface(IID,Obj)  then
    Result:=0
  else
    Result:=Windows.E_NoInterface;
end;

function TMan._AddRef:Integer;
begin
  Result:= -1
end;

function TMan._Release:Integer;
begin
  Result:= -1
end;

function TMan.SayHello:string;
begin
  Result:='Hello';
end;

procedure TForm1.Greeting(Intf:IGreetable);
begin
  ShowMessage(Intf.SayHello);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Intf:IGreetable;
begin
  Intf:=TMan.Create;
  Greeting(Intf);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -