如何通过interface 实现多重继承??? ( 积分: 100 )

  • 主题发起人 主题发起人 happydata2005
  • 开始时间 开始时间
H

happydata2005

Unregistered / Unconfirmed
GUEST, unregistred user!

看下面的代码

type

IMyInterface = interface
procedure P1

procedure P2

end

TMyImplClass = class
procedure P1

procedure P2

end

TMyClass = class(TInterfacedObject, IMyInterface)
FMyImplClass: TMyImplClass

property MyImplClass: TMyImplClass read FMyImplClass implements IMyInterface

procedure IMyInterface.P1 = MyP1

procedure MyP1


end

procedure TMyImplClass.P1

...
procedure TMyImplClass.P2

...
procedure TMyClass.MyP1

...
var
MyClass: TMyClass

MyInterface: IMyInterface

begin
MyClass := TMyClass.Create

MyClass.FMyImplClass := TMyImplClass.Create

MyInterface := MyClass

MyInterface.P1
// calls TMyClass.MyP1

MyInterface.P2
// calls TImplClass.P2

end;

其中我觉得
procedure IMyInterface.P1 = MyP1

procedure MyP1

这样写起来太繁琐了
能不能直接在继承类调用接口的方法 并且实现他?
 

看下面的代码

type

IMyInterface = interface
procedure P1

procedure P2

end

TMyImplClass = class
procedure P1

procedure P2

end

TMyClass = class(TInterfacedObject, IMyInterface)
FMyImplClass: TMyImplClass

property MyImplClass: TMyImplClass read FMyImplClass implements IMyInterface

procedure IMyInterface.P1 = MyP1

procedure MyP1


end

procedure TMyImplClass.P1

...
procedure TMyImplClass.P2

...
procedure TMyClass.MyP1

...
var
MyClass: TMyClass

MyInterface: IMyInterface

begin
MyClass := TMyClass.Create

MyClass.FMyImplClass := TMyImplClass.Create

MyInterface := MyClass

MyInterface.P1
// calls TMyClass.MyP1

MyInterface.P2
// calls TImplClass.P2

end;

其中我觉得
procedure IMyInterface.P1 = MyP1

procedure MyP1

这样写起来太繁琐了
能不能直接在继承类调用接口的方法 并且实现他?
 
后退
顶部