随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

unit Unit1;

interface

uses
  Classes, Controls, Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

  TBase = class
    procedure proc1; virtual;
    procedure proc2;
  end;

  TChild = class(TBase)
    procedure proc1; override;
    procedure proc2;
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TBase }

procedure TBase.proc1;
begin
  ShowMessage('B1');
end;

procedure TBase.proc2;
begin
  ShowMessage('B2');
end;

{ TChild }

procedure TChild.proc1;
begin
  //inherited;
  ShowMessage('C1');
end;

procedure TChild.proc2;
begin
  ShowMessage('C2');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  B: TBase;
  C: TChild;
  BC: TBase;
begin
  B := TBase.Create;
  C := TChild.Create;
  BC := TChild.Create;

  B.proc1;          //B1
  B.proc2;          //B2
  TChild(B).proc1;  //B1
  TChild(B).proc2;  //C2

  C.proc1;          //C1
  C.proc2;          //C2
  TBase(C).proc1;   //C1
  TBase(C).proc2;   //B2

  BC.proc1;         //C1
  BC.proc2;         //B2
  TBase(BC).proc1;  //C1
  TBase(BC).proc2;  //B2
  TChild(BC).proc1; //C1
  TChild(BC).proc2; //C2

  B.Free;
  C.Free;
  BC.Free;
end;

end.

posted on 2007-12-17 13:43  万一  阅读(...)  评论(...编辑  收藏