加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net – 如果接口定义了ReadOnly属性,实现者如何将Setter提供

发布时间:2020-12-17 00:22:11 所属栏目:大数据 来源:网络整理
导读:是否有一种方法可以为接口的实现者定义ReadOnly属性,使其成为完整的读/写属性? 想象一下,我定义了一个接口来提供ReadOnly属性(即,只是给定值的getter): Interface SomeInterface 'the interface only say that implementers must provide a value for read
是否有一种方法可以为接口的实现者定义ReadOnly属性,使其成为完整的读/写属性?

想象一下,我定义了一个接口来提供ReadOnly属性(即,只是给定值的getter):

Interface SomeInterface

    'the interface only say that implementers must provide a value for reading
    ReadOnly Property PublicProperty As String

End Interface

这意味着实施者必须承诺提供价值.但我希望给定的实现者也允许设置该值.在我看来,这意味着提供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

但这不会编译,因为对于VB编译器,实现者不再实现接口(因为它不再是ReadOnly).

从概念上讲,这应该可行,因为,最后,它只是意味着从接口实现getter,并添加一个setter方法.对于“正常方法”,这不是问题.

是否有某种方法可以实现它,而不使用“接口隐藏”或“自制”SetProperty()方法,并且具有属性的样式在实现中的行为类似于读/写属性?

谢谢 !

–UPDATE–
(我已经提出这个问题to a separate Question)
我的问题是:“为什么不能在VB.NET中完成”,当以下内容在C#.NET中有效时?“:

interface IPublicProperty
{
    string PublicProperty { get; }
}

实施:

public class Implementer:IPublicProperty
    {
        private string _publicProperty;

        public string PublicProperty
        {
            get
            {
                return _publicProperty;
            }
            set
            {
                _publicProperty = value;
            }
        }
    }
现在在Visual Studio 2015中受支持.

What’s New for Visual Basic

Readonly Interface Properties

You can implement readonly interface properties using a readwrite property. The interface guarantees minimum functionality,and it does not stop an implementing class from allowing the property to be set.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读