有没有办法使值只能由嵌套类 VB.NET 的父级访问?
总的来说,根据OOP范式,我对封装的理解基本上是这样的:
- 如果一个成员是私有的,那么它只能被类访问。
- 如果成员受保护,则只能由基类和任何派生类访问它。
- 如果成员是公开的,则任何人都可以访问。
如果我有一个嵌套类,我可以声明一个属性只能由该类及其嵌套的父类访问吗? 例如:
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
在示例中,如果 myNestedObject.myInt 或 myNestedObject.myDbl 成员是 Private 或 Protected,我无法从 ContainerClass 的实例直接访问这些成员。 但是假设我不想将它们公开,因为这样它们就太暴露了:它们可以从任何地方更改,而不仅仅是在 ContainerClass 对象内。 声明它们为“朋友”仍然太弱,因为这将允许从应用程序中的任何位置更改它们。
有什么办法可以实现我在这里的目标吗? 如果没有,有人能想出更明智的方法来实现这样的目标吗?
In general, according to the OOP paradigm, my understanding of encapsulation basically says:
- If a member is private, it can only be accessed by the class.
- If a member is protected, it can only be accessed by the base class and any derived classes.
- If a member is public, it can be accessed by anyone.
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法通过辅助功能修饰符的组合直接执行此操作。
我能想到的最好方法如下。 它涉及额外的间接级别。
现在父类并且只有父类才能访问这些字段属性和方法。
例如:
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.
Now the parent class and only the parent class will have access to those properties and methods.
For Example:
根据 JaredPar 的回答,您可以使用 Private ChildClass 但使用仅显示其应显示内容的公共接口:
用法:
Based on JaredPar's answer, you could use a Private ChildClass but a Public Interface that reveals only what it sould show :
Usage :