尝试在vb.net中实现接口时出现此错误:
Public Interface IFoo
ReadOnly Property Foo() As String
End Interface
Public Class myFoo
Implements IFoo
Public ReadOnly Property Foo() As String
Get
return "Foo"
End Get
End Property
...
End Class
Run Code Online (Sandbox Code Playgroud)
缺什么?
对于接口的实现者,是否有一种方法ReadOnly可以定义属性以使其成为完整的读/写Property?
想象一下,我定义了一个接口来提供一个ReadOnly Property(即,只是一个给定值的getter):
Interface SomeInterface
'the interface only say that implementers must provide a value for reading
ReadOnly Property PublicProperty As String
End Interface
Run Code Online (Sandbox Code Playgroud)
这意味着实施者必须承诺提供价值.但我希望给定的实现者也允许设置该值.在我的脑海中,这意味着提供Property的setter作为实现的一部分,做这样的事情:
Public Property PublicProperty As String Implements SomeInterface.PublicProperty
Get
Return _myProperty
End Get
Set(ByVal value As String)
_myProperty = value
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
但这不会编译,因为对于VB编译器,实现者不再实现接口(因为它不再是ReadOnly).
从概念上讲,这应该可行,因为,最后,它只是意味着从接口实现getter,并添加一个setter方法.对于"正常方法",这没有问题.
有没有一种方法可以做到这一点,而不采用"接口隐藏"或"自制" SetProperty()方法,以及Property在实现中具有类似读/写属性的样式?
谢谢 !
--UPDATE-- (我把这个问题转移到一个单独的问题)我的问题是:"为什么不能在VB.NET中完成",当以下内容在C#.NET中有效时?":
interface IPublicProperty
{ …Run Code Online (Sandbox Code Playgroud)