delphi - 接口(interface)和属性可见性

标签 delphi oop interface delphi-7

我有一个简单的界面

  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/

相关文章:

delphi - 如何在使用具有非特定库路径的 Delphi 时删除重复资源(RES、DFM)?

Delphi,使用vbscript连接ADO,new Be

java - 基于参数真实类型的重载方法选择

python - 在类中运行单元测试

c++ - 在 OOP 中,什么是转发,它与委托(delegate)有何不同?

Java:接口(interface)和工厂中的泛型

delphi - `const` 的使用是教条性的还是理性的?

c# - 通过 switch 语句处理对象集合

interface - 使用 Dart/Flutter 导入 Umbrella

delphi - 更改宽度 SysIPAddress32