理解COM

unit COMCodeExample1_Unit;

{Project     : Abstract Method Example
Programming : Curtis W. Socha
Date        : 12/03/2000
References  : Delphi COM Programming

This coding example will attempt to explain the basic concepts
needed to understand how the reserved word "interface" works
and how this will lead to understanding how COM objects function.
}

interface

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

type
  //This is our class that will only contain an
  //abstract|virtual method. No data is allowed in a
  //real interface so for the purposes of understanding the
  //concept, TFormattedNumber will not contain any data.
  //
  //Virtual is used so that the function is placed in the
  //VMT so it can be overridden by sub-classes. Many standard
  //Delphi component methods can be overriden because they
  //too are declared virtual and are placed in the VMT.
  //
  //Abstract is used do tell the compiler that there is no
  //implementation for this function, only that it exists
  //and will be implemented somewhere else.
  TFormattedNumber = class
  public
    function FormattedString : string; virtual; abstract;
  end;


  //This class inherits the abstract method FormattedString from
  //TFormattedNumber. It will override the method so that the
  //sub-class will implement the functionality as we shall see later.
  TFormattedInteger = class(TFormattedNumber)
  public
    FIntValue : Integer;
    function FormattedString : string; override;
  end;


  //This class also inherits the abstract method FormattedString from
  //TFormattedNumber. It will override the method so that the
  //sub-class will implement the functionality as we shall see later.
  TFormattedDouble = class(TFormattedNumber)
  public
    FDblValue : Double;
    function FormattedString : string; override;
  end;


  TProjectForm = class(TForm)
    Label1: TLabel;
    IntegerEntry: TEdit;
    Label2: TLabel;
    DoubleEntry: TEdit;
    DisplayButton: TButton;
    procedure DisplayButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure DisplayValue(FormattedNumber : TFormattedNumber);
  end;

var
  ProjectForm: TProjectForm;

implementation

{$R *.DFM}
{----------------------------------------------------------------------------------}
{ TFormattedInteger }
//This is the implementation of the overriden FormattedString
//method for our TFormattedInteger Class.
function TFormattedInteger.FormattedString : string;
begin
  Result := IntToStr(FIntValue);
end;

{----------------------------------------------------------------------------------}
{ TFormattedDouble }
//This is the implementation of the overriden FormattedString
//method for our TFormattedDouble Class.
function TFormattedDouble.FormattedString : string;
begin
  Result := FloatToStr(FDblValue);
end;

{---------------------------------------------------------------------------------}
{ TProjectForm }
//This procedure will accept any object that is a decendent
//of TFormattedNumber and will call it's overriden method.
//I have placed a TempString in here so that you can
//see what is what by using the reserved word "is".
procedure TProjectForm.DisplayValue(FormattedNumber: TFormattedNumber);
var
  TempString : string;
begin
  if FormattedNumber is TFormattedInteger then     //Am I a TFormattedInteger?
    TempString := 'FIntValue is = '
  else if FormattedNumber is TFormattedDouble then //Am I a TFormattedDouble?
    TempString := 'FDblValue is = ';

  ShowMessage(TempString + FormattedNumber.FormattedString);
end;
{----------------------------------------------------------------------------------}
//This is what actually makes it go.
//I instantiate two separate classes, one for TFormattedInteger
//and one for TFormattedDouble. I then populate their
//internal values. I then call DisplayValue.
procedure TProjectForm.DisplayButtonClick(Sender: TObject);
var
  FInt : TFormattedInteger;
  FDbl : TFormattedDouble;
begin
  {Create em}
  FInt := TFormattedInteger.Create;
  FDbl := TFormattedDouble.Create;

  {Populate em}
  FInt.FIntValue := StrToInt(IntegerEntry.Text);
  FDbl.FDblValue := StrToFloat(DoubleEntry.Text);

  {Display em}
  DisplayValue(FInt);
  DisplayValue(FDbl);

  {Free em}
  FInt.Free;
  FDbl.Free;
end;
{----------------------------------------------------------------------------------}

end.

image

转载于:https://www.cnblogs.com/xxd0825/archive/2012/12/19/2824993.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值