如何在VB.NET中将接口属性设置为只读为只读?

时间:2008-10-27 14:13:20

标签: vb.net oop interface

这是我之前关于接口的问题的后续行动。我收到了一个我喜欢的答案,但我不确定如何在VB.NET中实现它。

上一个问题:

Should this property be part of my object's interface?

public interface Foo{
  bool MyMinimallyReadOnlyPropertyThatCanAlsoBeReadWrite {get;}
}

如何使用VB.NET语法实现这一目标?据我所知,我唯一的选择是将属性标记为ReadOnly(我无法实现setter)或不(我必须实现setter)。

2 个答案:

答案 0 :(得分:2)

只需在一个界面中定义getter,然后创建一个同时具有getter和setter的第二个接口。如果您的具体类是可变的,请让它实现第二个接口。在处理类的代码中,检查它是否是第二个接口的实例,如果是,则调用setter。

答案 1 :(得分:1)

在VB.NET中,我会这样实现它:

Public Interface ICanBeSecure

    ReadOnly Property IsSecureConnection() As Boolean
End Interface

Public Interface IIsSecureable
    Inherits ICanBeSecure

    Shadows Property IsSecureConnection() As Boolean
End Interface