unit UCmp;
interface
type
TCmp=class
private
procedure ProCmp;
public
function FunCmp: integer;
end;
implementation
{ TCmp }
function TCmp.FunCmp: integer;
begin
//Cmp不可见
end;
procedure TCmp.ProCmp;
begin
//Cmp不可见
end;
end.
************************
unit UTest1;
interface
uses
Dialogs,UCmp, Classes;
type
Test1 =class(TComponent)
private
procedure ProTest1;
protected
procedure Test1Protected;
public
function FunTest1: integer;
end;
implementation
{ Test1 }
function Test1.FunTest1: integer;
begin
showmessage('Cmp is Test1 now!');
end;
procedure Test1.ProTest1;
begin
////Cmp不可见
end;
procedure Test1.Test1Protected;
begin
//Cmp不可见
end;
end.
*************************
unit UTest2;
interface
uses
Dialogs,UCmp;
type
Test2 =class(TCmp)
private
procedure ProTest2;
protected
procedure Test2Protected;
public
function FunTest2: integer;
end;
implementation
{ Test2 }
function Test2.FunTest2: integer;
begin
showmessage('Cmp is Test2 now!');
end;
procedure Test2.ProTest2;
begin
//
end;
procedure Test2.Test2Protected;
begin
//
end;
end.
******
unit main;
interface
uses
UTest1, UTest2, UCmp,Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
Cmp: TCmp;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
begin
Cmp:=TCmp.Create;
Test1(Cmp).FunTest1;
Test2(Cmp).FunTest2;
end;
end.
上面的代码执行成功!试试!by the way 好好的多态性为何不用?