在实践中真的会发现更多问题.

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

  IA = Interface
    function GetName: string;
    property Name: string read GetName;
  end;

  TC1 = class(TInterfacedObject, IA)
    function GetName: string; virtual;
  end;

  TC2 = class(TC1)
    function GetName: string; override; //覆盖
  end;

  TC3 = class(TC2)
    function GetName: string; override; //再覆盖
  end;

  TC4 = class(TC3)
    function GetName: string; override; //再覆盖
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TC1 }

function TC1.GetName: string;
begin
  Result := 'C1';
end;

{ TC2 }

function TC2.GetName: string;
begin
  Result := 'C2';
end;

{ TC3 }

function TC3.GetName: string;
begin
  Result := 'C3';
end;

{ TC4 }

function TC4.GetName: string;
begin
  Result := inherited + '0';
end;


{测试}
procedure TForm1.FormCreate(Sender: TObject);
var
  v1,v2,v3,v4: IA;
begin
  v1 := TC1.Create;
  v2 := TC2.Create;
  v3 := TC3.Create;
  v4 := TC4.Create;
  ShowMessageFmt('%s, %s, %s, %s', [v1.Name, v2.Name, v3.Name, v4.Name]); //C1, C2, C3, C30
end;

end.

posted on 2012-01-03 22:09  万一  阅读(2554)  评论(2编辑  收藏  举报