type
Iinterface = interface
['{65F4679B-653A-4594-9301-2812991E6FCF}']
function QueryInterface(const IID : TGUID; out Obj): Hresult; stdcall;
function _addRef:integer;stdcall;
function _Release : Integer; Stdcall;
end;IunKowm = IInterface;type
IJuCanSwim = interface(IInterface)
['{6FE0695A-BDF0-4833-B353-C45BBE26DF8F}']
function Getplace():string;
procedure setplace(const Value : String);
function Swim():string;
property Place : string read Getplace write Setplace;end;type
TJuFish = Class(TInterfacedObject , IjuCanSwim)
private
Fplace : string;
public
function GetPlace():string;
procedure SetPlace(const Value : string);
function SWim():string;virtual;
end;
//接口的继承实现
type
TJusalmon = class(TjuFish)
public
function SWim():string;override;
end;
var
Form1: TForm1;implementation{$R *.dfm}{ TJuFish }
function TJuFish.SWim: string;
begin
Result := 'Yes . I can Swim';
end;function TJusalmon.SWim: string;
begin
result := 'in sky';
end;
procedure TForm1.Button3Click(Sender: TObject);
var
CanSwim : IjucanSwim;
begin
Memo1.Clear;
CanSwim := Tjufish.Create();
CanSwim.Place := 'in river';
Memo1.Lines.Add('IjuCanSwim::');
Memo1.Lines.Add(CanSwim.Swim() + CanSwim.Place);
end;procedure TForm1.Button4Click(Sender: TObject);
var
CanSwim : IjucanSwim;
begin
Memo1.Clear;
CanSwim := Tjusalmon.Create();
CanSwim.Place := 'in river';
Memo1.Lines.Add(CanSwim.Swim() + CanSwim.Place);
end;{ TJusalmon }end.
大概意思是这样的,首先定义一个接口,然后再从这个接口继承,然后派生出一个类,然后又从这个类派生一个类,第一个类已经实现了SWIM,但是我又在下一个类覆盖了SWIM,为什么CanSwim := Tjusalmon.Create();和CanSwim := Tjufish.Create();的结果一样,而不是result := 'in sky';代码有点多,希望大吓能够指点
1. CanSwim := Tjufish.Create()結果是"Yes . I can Swim in rive
2. CanSwim := Tjusalmon.Create()結果是"in sky in river "