4

我有一个简单的界面

  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 方法消失了,只有属性 Port 是可见的——太好了。

现在我实现一个接口

  TSomeProperties = class(TInterfacedObject, ISomeProperties)
  private
    function GetPort: integer;
    procedure SetPort(const Port: integer);
  end;

但是属性Port在实现接口的类中是不可见的!这是理想的行为还是我做错了什么?

4

1 回答 1

4

类不是接口。接口上的属性只是语法糖,暴露了 Delphi 的 GetPort 和 SetPort 方法。

这个属性并不是真正必须或可以实现的东西(只有访问器方法),所以它在实现类中是不可见的,除非你也在那里定义了一个属性。您唯一可以实现的就是方法。

FWIW,当您定义属性时,这些方法不会“消失”。你仍然可以打电话给他们。接口的所有成员都具有相同的可见性。

于 2013-02-19T16:57:22.670 回答