我有一个简单的界面
ISomeProperties = interface
['{3AD52E4E-5190-4ABE-8AFC-062295E3A352}']
function GetPort: integer;
procedure SetPort(const Port: integer);
end;
GetFunction 和 SetFunction 在代码完成中可见。但是在我添加这样的属性之后
ISomeProperties = interface
['{3AD52E4E-5190-4ABE-8AFC-062295E3A352}']
function GetPort: integer;
procedure SetPort(const Port: integer);
property Port: integer read GetPort write SetPort;
end;
GetPort 和 SetPort 方法消失,只有属性端口可见 - 太棒了。
现在我实现一个接口(interface)
TSomeProperties = class(TInterfacedObject, ISomeProperties)
private
function GetPort: integer;
procedure SetPort(const Port: integer);
end;
但是属性Port对于实现该接口(interface)的类是不可见的!这是我们想要的行为还是我做错了什么?
最佳答案
类不是接口(interface)。接口(interface)上的属性只是语法糖,公开了 Delphi 的 GetPort 和 SetPort 方法。
此属性并不是真正必须或可以实现的东西(仅是访问器方法),因此它在实现类中不可见,除非您也在那里定义了一个属性。您唯一可以实现的就是方法。
FWIW,当您定义属性时,这些方法不会“消失”。您仍然可以给他们打电话。接口(interface)的所有成员都具有相同的可见性。
关于delphi - 接口(interface)和属性可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14962700/