如果接口定义了 ReadOnly 属性,那么实现者如何为该属性提供 Setter? [英] If an interface defines a ReadOnly Property, how can an implementer provide the Setter to this property?

查看:21
本文介绍了如果接口定义了 ReadOnly 属性,那么实现者如何为该属性提供 Setter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接口的实现者有没有办法定义一个ReadOnly 属性,使其成为一个完整的读/写属性?

Is there a way for implementers of an interface where a ReadOnly property is defined to make it a complete Read/Write Property ?

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

Imagine I define an interface to provide a ReadOnly Property (i.e., just a getter for a given value) :

Interface SomeInterface

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

End Interface

这意味着实施者必须承诺提供价值.但我希望给定的实施者也允许设置该值.在我看来,这意味着提供 Property 的 setter 作为实现的一部分,做这样的事情:

This means implementers must commit to providing a value. But I would like a given implementer to also allow setting that value. In my head, this would mean providing the Property's setter as part of the implementation, doing something like this :

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).

but this will not compile as, for the VB compiler, the implementer no longer implements the interface (because it is no longer ReadOnly).

从概念上讲,这应该可行,因为最终它只是意味着从接口实现 getter,并添加一个 setter 方法.对于普通方法",这没问题.

Conceptually, this should work, because, in the end, it just means Implement the getter from the Interface, and add a setter method. For "normal methods", this would be no problem.

有没有办法做到这一点,而不诉诸界面隐藏"或自制"SetProperty() 方法,并且使 Property 表现得像实现中的读/写属性?

Is there some way of doing it, without resorting to "interface hiding" or "home-made" SetProperty() method, and style having the Property behave like a Read/Write property in the implementations ?

谢谢!

--更新--(我已经移动了这个问题 到一个单独的问题)我的问题真的是:为什么不能在 VB.NET 中做到这一点",当以下内容在 C#.NET 中有效时?":

--UPDATE-- (I have moved this question to a separate Question) My question is really : "why can't this be done in VB.NET", when the following is valid in C#.NET?" :

interface IPublicProperty
{
    string PublicProperty { get; }
}

实施:

public class Implementer:IPublicProperty
    {
        private string _publicProperty;

        public string PublicProperty
        {
            get
            {
                return _publicProperty;
            }
            set
            {
                _publicProperty = value;
            }
        }
    }

推荐答案

最后,我得到了一个符合我目标的解决方案:

In the end, I ended up with a solution that matches my goal :

  • 通过接口访问的用户至少会看到一个 getter
  • 访问实现的用户可以读取和写入.

我像这样遮蔽"了实现的属性:

'users who access through interface see only the Read accessor
Public ReadOnly Property PublicProperty_SomeInterface As String Implements SomeInterface.PublicProperty
    Get
        Return _myProperty
    End Get
End Property


'users who work with the implementation have Read/Write access
Public Property PublicProperty_SomeInterface As String
    Get
        Return _myProperty
    End Get
    Set(ByVal value As String)
        _myProperty = value
    End Set
End Property

然后,根据您的使用方式,您可以这样做:

Then, depending on how you use it, you can do :

Dim implementorAsInterface As SomeInterface = New InterfaceImplementor()
logger.Log(implementor.PublicProperty) 'read access is always ok
implementor.PublicProperty = "toto" 'compile error : readOnly access

Dim implementor As InterfaceImplementor = New InterfaceImplementor()
implementor.PublicProperty = "toto" 'write access

这篇关于如果接口定义了 ReadOnly 属性,那么实现者如何为该属性提供 Setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆