【发布时间】:2016-01-08 18:14:26
【问题描述】:
我正在为我正在编写的杂货应用程序编写一个 VB.NET 类库,但我认为我对 OOP 在 VB.NET 中的工作方式存在误解。我原以为如果class x 在class y 中,那么class x 的instance 也将在class y 的instance 中,但显然不是案子。我将如何设置它以能够通过class y 访问class x 的实例?还有为什么实例 y 中没有实例 x?
(更新:我的意思是这个)
Public Class y
Public Class x //class inside of class
End Class
End Class
Public Class Form1
Public Sub Form1_Load(<params>) Handles Me.Load
Dim yinst As y = New y()
Dim xinst As x = New y.x()
MsgBox(yinst.xinst) //instance inside of instance
End Sub
End Class
【问题讨论】:
-
请举例说明你说class x was in class y
-
这称为嵌套类,X 与 Y 不同,因为 Y 可以不仅仅是 X 的包装器。例如,
X不包含另一个X。New x()代码在真实的应用程序中不起作用,它必须是:New y.x() -
如何在 y 下添加 x 的实例?
-
一些额外的官方.net recommendations
标签: vb.net oop inner-classes