在另一个类中声明一个类的目的是什么? [英] What is the purpose of declaring a Class within another Class?

查看:69
本文介绍了在另一个类中声明一个类的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自VBA世界,在那里将代码分解为类,名称空间和模块的选项受到限制。现在我刚进入一个有很多选择的世界,我感到迷茫。

I come from the VBA world where options to breakdown your code into classes, namespaces, and modules is limited. Now I just landed in a world where the options are many, and I feel lost.

我想知道在另一个类中声明一个类的目的是什么? (请参见下面的示例)

I would like to know what is the purpose of declaring a Class within another Class? (see example below)

Class FirstClass
    Public OnePropertyInside As String
    Class SecondClass
        Public AnotherProperty As String
    End Class
End Class

如果我创建新实例 FirstClass (例如 myFirstClass )的实例, SecondClass 未实例化。
(至少对我来说更奇怪)是intelissense为我提供了 myFirstClass.SecondClass 。显然,因为未实例化该类,所以我无法访问其任何成员。

If I create a new instance of FirstClass (say myFirstClass), SecondClass is not instantiated. Even more bizzare (to me at least), is that intelissense offers me myFirstClass.SecondClass. Obviously, because the class is not instantiated, I cannot access any of its members.

因此,仅当 SecondClass 是否包含个共享成员?
为了尝试回答这个问题,我在 SecondClass 中添加了一个共享成员:

So, is that usefull only if the SecondClass contains shared members? To try answering that question I added a shared member within SecondClass:

Class FirstClass
    Public OnePropertyInside As String
    Class SecondClass
        Public AnotherProperty As String
        Public Shared SharedProperty As String
    End Class
End Class

我进行了一些测试,这些测试带来了次要问题(请参见代码中的注释)

I ran a few tests which brought secondary questions (see comments in code)

Sub Main()       

    Dim myFirstClass As New FirstClass
    'Works as expected
    Console.WriteLine(myFirstClass.OneProperty)

    'What is the difference between the two lines below?
    Console.WriteLine(myFirstClass.SecondClass.SharedProperty)
    Console.WriteLine(FirstClass.SecondClass.SharedProperty)

    'This line cannot be compiled, this demonstrates SecondClass is not instantiated when FirstClass is.
    Console.WriteLine(myFirstClass.SecondClass.AnotherProperty)

    Dim mySecondClass As New FirstClass.SecondClass
    'Works as expected, but I feel this hierarchy should better be dealt with through a namespace statement?
    Console.WriteLine(mySecondClass.AnotherProperty)

End Sub


推荐答案

执行此操作后,其他类即可访问内部类(可访问性为公共朋友),外部类基本上就像命名空间一样工作。因此,例如,使用您的示例,您可以创建嵌套类的新对象,而无需创建任何外部类:

When you do that, and the inner class is accessible to other classes (it's accessibility is Public or Friend), the outer class basically just works like a namespace. So for instance, using your example, you could create a new object of the nested class without ever creating one of the outer class:

Dim x As New FirstClass.SecondClass()

最明显的好处是代码的结构化组织,很像名称空间和代码文件。因此,例如,对常量使用嵌套类以帮助更好地组织它们并不罕见:

The most obvious benefit is the structural organization of the code, much like namespaces and code files. So, for instance, it's not uncommon to use nested classes for constants, to help better organize them:

Public Class Urls
    Public Class Processing
        Public Const Submit As String = "..."
        Public Const Cancel As String = "..."
    End Class

    Public Class Reporting
        Public Const Daily As String = "..."
        Public Const Weekly As String = "..."
    End Class
End Class

' ...

Dim url As String = Urls.Reporting.Daily

但是,在狭窄的情况下,诸如此类的事情很有用,大多数人宁愿根本不嵌套公共类。

However, outside of the narrow set of situations where things like that are useful, most people would prefer to not nest public classes at all.

但是,正如其他人提到的那样,您真正会看到经常使用嵌套类的地方是 Private 个。如果您需要一些小型的帮助程序类,而该类无需在您的课程之外进行编码,则无需公开它。即使您将其设置为对 Friend 的可访问性,它仍将对同一项目中的所有其他类可见。因此,如果您真的想将其隐藏在其他所有内容中,则需要使其成为嵌套的私有类。例如:

However, as others have mentioned, the one place where you really will see nested classes used fairly regularly is for Private ones. If you need some small helper class which will have no use to code outside of your class, there's no reason to expose it. Even if you set it's accessibility to Friend, it will still be visible to all the other classes in the same project. Therefore, if you really want to hide it from everything else, you'll want to make it a nested private class. For instance:

Public Class MyClass
    Public Function GetTheIdOfSomething() As Integer
        Dim d As Details = GetDetailsAboutSomething()
        If d.Value Is Nothing Then
            Return d.Id
        Else
            Throw New Exception()
        End If
    End Sub

    Private Function GetDetailsAboutSomething() As Details
        ' ... return a Details object
    End Function

    Private Class Details
        Public Property Id As Integer
        Public Property Value As String
    End Class
End Class

这篇关于在另一个类中声明一个类的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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