是否有 Delphi 等同于 Java 接口 ("class ... implements")?
Is there a Delphi equivalent to Java interfaces ("class ... implements")?
假设我有两个 类 “mycheckbox” 和 “mymemo” 来自 TCheckBox
和 TMemo
分别。我想为标题为“isdone”的两者创建一个方法,该方法检查组件是否已完成。有没有一种方法可以在不知道正在处理两个 类 方法中的哪一个的情况下访问该方法?
在 Java 中将是:class mycheckbox implements MyInterface
在 Delphi 中,您将使用通用界面,就像在 Java 中一样,例如:
type
IMyInterface = interface
['{bfa4fffc-b87e-49ce-8aa9-4911e106959c}']
function IsDone: Boolean;
end;
MyCheckBox = class(TCheckBox, IMyInterface)
public
function IsDone: Boolean;
end;
MyMemo = class(TMemo, IMyInterface)
public
function IsDone: Boolean;
end;
function MyCheckBox.IsDone: Boolean;
begin
Result := ...;
end;
function MyMemo.IsDone: Boolean;
begin
Result := ...;
end;
procedure DoSomething(Intf: IMyInterface);
begin
...
if Intf.IsDone then...
...
end;
假设我有两个 类 “mycheckbox” 和 “mymemo” 来自 TCheckBox
和 TMemo
分别。我想为标题为“isdone”的两者创建一个方法,该方法检查组件是否已完成。有没有一种方法可以在不知道正在处理两个 类 方法中的哪一个的情况下访问该方法?
在 Java 中将是:class mycheckbox implements MyInterface
在 Delphi 中,您将使用通用界面,就像在 Java 中一样,例如:
type
IMyInterface = interface
['{bfa4fffc-b87e-49ce-8aa9-4911e106959c}']
function IsDone: Boolean;
end;
MyCheckBox = class(TCheckBox, IMyInterface)
public
function IsDone: Boolean;
end;
MyMemo = class(TMemo, IMyInterface)
public
function IsDone: Boolean;
end;
function MyCheckBox.IsDone: Boolean;
begin
Result := ...;
end;
function MyMemo.IsDone: Boolean;
begin
Result := ...;
end;
procedure DoSomething(Intf: IMyInterface);
begin
...
if Intf.IsDone then...
...
end;