有没有一种方法可以使值只能由嵌套类VB.NET的父级访问? [英] Is there a way to make a value accessible only to the parent of a nested class VB.NET?

查看:81
本文介绍了有没有一种方法可以使值只能由嵌套类VB.NET的父级访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,根据OOP范例,我对封装的理解基本上是:

In general, according to the OOP paradigm, my understanding of encapsulation basically says:


  1. 如果成员是私有成员,则只能

  2. 如果成员受保护,则只能由基类和任何派生类访问。

  3. 如果成员是公共的,任何人都可以访问它。

如果我有嵌套类,可以声明一个属性只能访问该类及其嵌套的父类?例如:

If I have a nested class, can I declare a property to be accessible only to that class and the parent class it's nested within? For example:

Public Class ContainerClass
    Public Class NestedClass
        Protected myInt As Integer ' <- this is what I am wondering about '
        Protected myDbl As Double ' <- this is what I am wondering about '

        Sub New()
            myInt = 1
            myDbl = 1.0
        End Sub
    End Class

    Private myNestedObject As New NestedClass

    ' this function is illegal '
    Public Sub GrowNestedObject(ByVal multiplier As Integer)
        myNestedObject.myInt *= multiplier
        myNestedObject.myDbl *= multiplier
    End Sub
End Class

在此示例中,如果那些成员是Private或Protected,则无法直接从ContainerClass实例访问myNestedObject.myInt或myNestedObject.myDbl。但是,假设我不想将它们设为公开,因为这样它们就会被暴露在外:可以在任何地方进行更改,而不仅仅是在ContainerClass对象中进行更改。声明它们为Friend仍然太弱,因为那样可以允许它们在应用程序中的任何位置进行更改。

In the example, I cannot directly access myNestedObject.myInt or myNestedObject.myDbl from an instance of ContainerClass if those members are Private or Protected. But suppose I don't want to make them Public, because then they are TOO exposed: they can be altered from anywhere, not just within a ContainerClass object. Declaring them Friend would still be too weak as that would allow them to be altered from anywhere within the application.

有什么方法可以完成我在这里要做什么?如果没有,那么谁能想到一种更明智的方法来实现这样的目标?

Is there any way to accomplish what I am going for here? If not, can anyone think of a more sensible way to achieve something like this?

推荐答案

没有办法直接做到这一点

There is no way of doing this directly with a combination of accessibility modifiers.

我能想到的最好方法如下。它涉及一个额外的间接级别。

The best way I can think of doing this is as follows. It involves an extra level of indirection.


  • 创建具有私有可访问性的嵌套接口。这将仅授予父类和嵌套子类访问权限

  • 将要访问的字段添加到该接口

  • 使嵌套类实现该接口

  • 使所有实现都具有私有可访问性

  • Create a Nested Interface with Private accessibility. This will give only the Parent class and nested children access
  • Add the fields you want access to to that interface
  • Make the Nested class implement the interface
  • Make all of the implementations have private accessibility

现在是父类,只有父类该类将有权访问这些属性和方法。

Now the parent class and only the parent class will have access to those properties and methods.

例如:

Class Parent
    Private Interface Interface1
        ReadOnly Property Field1() As Integer
    End Interface

    Public Class Nested1
        Implements Interface1

        Private ReadOnly Property Field1() As Integer Implements Interface1.Field1
            Get
                Return 42
            End Get
        End Property
    End Class

    Sub New()
        Dim child As Interface1 = New Nested1
        Dim x = child.Field1
    End Sub
End Class

这篇关于有没有一种方法可以使值只能由嵌套类VB.NET的父级访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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