Ste*_*eve 4 delphi oop interface
这两段代码之间有什么区别
type
IInterface1 = interface
procedure Proc1;
end;
IInterface2 = interface
procedure Proc2;
end;
TMyClass = class(TInterfacedObject, IInterface1, IInterface2)
protected
procedure Proc1;
procedure Proc2;
end;
Run Code Online (Sandbox Code Playgroud)
以下内容:
type
IInterface1 = interface
procedure Proc1;
end;
IInterface2 = interface(Interface1)
procedure Proc2;
end;
TMyClass = class(TInterfacedObject, IInterface2)
protected
procedure Proc1;
procedure Proc2;
end;
Run Code Online (Sandbox Code Playgroud)
如果它们是同一个,那么它们是否有任何优点或可读性问题.
我想第二个意味着你不能编写一个实现IInterface2的类而不实现IInterface1,而第一个你可以.
如果我们讨论的是Delphi for Win32(Delphi for .NET有不同的规则),那么两段代码的效果会有很大不同,并且几乎没有相同之处.
Supports
或' as
')将失败.在这个例子中看看你自己:
type
A_I1 = interface
end;
A_I2 = interface(A_I1)
end;
A_Class = class(TInterfacedObject, A_I2)
end;
procedure TestA;
var
a: A_Class;
x: A_I1;
begin
a := A_Class.Create;
x := a; // fails!
end;
type
B_I1 = interface
end;
B_I2 = interface
end;
B_Class = class(TInterfacedObject, B_I1, B_I2)
end;
procedure TestB;
var
a: B_Class;
x: B_I1;
begin
a := B_Class.Create;
x := a; // succeeds!
end;
begin
TestA;
TestB;
end.
Run Code Online (Sandbox Code Playgroud)