COM程序編寫(xiě)入門(mén)(二)

字號(hào):

考試大計(jì)算機(jī)等級(jí)站整理收集:
    COM的理論
    以實(shí)例來(lái)講COM的接口(Interface)是COM的核心,所有的COM接口都是通過(guò)IUnknown派生出來(lái)的,它告知客戶那些接口是有效的,即已經(jīng)被實(shí)現(xiàn)類(lèi)說(shuō)定義。它定義的一般方式如下:
    ISimpleInterface=Interface(IUnknown)
    Function GetName:String
    Procedure SetName(v_Name:String)
    End;
    如果在上面的接口中加入這樣一行:
    ISimpleInterface=Interface(IUnknown)
    V_Name:String;
    Function GetName:String
    Procedure SetName(v_Name:String)
    End;
    這樣是不被允許的,因?yàn)樯厦嫖覀冋f(shuō)到接口方法就像是一個(gè)占位符,需要實(shí)現(xiàn)類(lèi)引出才有實(shí)際意義,v_Name:String這一句只是一個(gè)數(shù)據(jù)成員將永遠(yuǎn)無(wú)任何意義,如果要定義也只能在實(shí)現(xiàn)類(lèi)中定義。
    現(xiàn)在舉一個(gè)COM的例子,沒(méi)有什么實(shí)際用處但至少說(shuō)明問(wèn)題:
    unit Unit1;
    interface
    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;
    type
    TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    private
    { Private declarations }
    public
    { Public declarations }
    end;
    ISimpleInterface=Interface(IUnknown)
    Procedure SetValue(v_Value:Integer);
    Function GetValue:Integer;
    End;