接口 VB.NET 中的读写属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14622335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:07:17  来源:igfitidea点击:

Read-write property in interface VB.NET

vb.netpropertiesinterface

提问by benst

I would like to write an interface class and use it like this

我想写一个接口类并像这样使用它

public Interface IFunction
  property name as string
end interface

public class ModelFunction
   implements IFunction

  public property name as string

  public sub new()

end class

*EDIT(deleted following sentence for being noob, thanks @damien_the_unbeliever for pointing this out):But this is not possible to get because a property in an vb.net interface has to be readonly or writeonly (as far as i get it)*

*编辑(删除了下面这句话,因为是菜鸟,感谢@damien_the_unbeliever 指出这一点):但是这是不可能的,因为 vb.net 接口中的属性必须是只读或只写(据我所知)*

I have now written this but seems a little wrong:

我现在已经写了这个,但似乎有点错误:

public Interface IFunction
  Readlonly property getName() as string
  writeonly property writeName() as string
end interface

public class ModelFunction
 implements IFunction

 ....
end class

Anyone have a better solution for this? or can help me out with properties in an Interface class. Have read some articles here on stackoverflow but none of them point me in the right direction.

有没有人对此有更好的解决方案?或者可以帮助我解决接口类中的属性。在这里阅读了一些关于 stackoverflow 的文章,但没有一篇为我指明了正确的方向。

回答by Damien_The_Unbeliever

This works fine for me:

这对我来说很好用:

Public Class Class1
    Implements Thing

    Property Gary As Int32 Implements Thing.Gary
        Get
            Return 10
        End Get
        Set(value As Int32)

        End Set
    End Property
End Class

Public Interface Thing
    Property Gary As Int32
End Interface

There's even an example on the documentation page for Interface:

文档页面上甚至还有一个示例Interface

Interface IAsset
    Event ComittedChange(ByVal Success As Boolean)
    Property Division() As String 
    Function GetID() As Integer 
End Interface