是什么原因之一可能需要使用嵌套类? [英] What are reasons why one would want to use nested classes?

查看:106
本文介绍了是什么原因之一可能需要使用嵌套类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<一个href=\"http://stackoverflow.com/questions/3299562/what-kind-of-access-modifiers-can-be-applied-to-a-class/3299588#3299588\">this StackOverflow的答案一个评论者指出,私人嵌套类是非常有用所以我在的articles比如这个往往解释类如何嵌套函数的技术上,但不是的为什么你会使用它们

In this stackoverflow answer a commenter mentioned that "private nested classes" can be quite useful so I was reading about them in articles such as this one which tend to explain how nested classes function technically, but not why you would use them.

我想我会用私人嵌套类的小帮手类属于一个较大的类,但我常常需要从另一个类的辅助类,所以我就只需要采取额外的为了(1)使嵌套类非嵌套或(2)将其公开,然后用在其上的外级preFIX,其中两个似乎是没有任何附加价值的额外工作对于具有访问它嵌套类摆在首位。因此,在一般的我真的没有看到嵌套类用例,其他的或许比让班多一点组织成组,但我也违背了一类 - 每个文件清楚,我来享受。

I suppose I would use private nested classes for little helper classes that belong to a larger class, but often I will need a helper class from another class and so I would just have to take the extra effort to (1) make the nested class non-nested or (2) make it public and then access it with the outer-class prefix on it, which both seems to be extra work without any added-value for having the nested class in the first place. Hence in general I really don't see a use case for nested classes, other than perhaps to keep classes a bit more organized into groups, but I that also goes against the one-class-per-file clarity that I have come to enjoy.

在哪些方面你使用嵌套类,让你的code更易于管理,可读性强,效率?

推荐答案

您已经回答了你自己的问题。当你需要一个辅助类,这是毫无意义的类外使用嵌套类;特别是当嵌套类可利用的外部类的私有实现细节。

You've answered your own question. Use nested classes when you need a helper class that is meaningless outside the class; particularly when the nested class can make use of private implementation details of the outer class.

您的论点,即嵌套类是没有用的也是一种观点认为私有方法都是无用:私有方法可能是类的有用之外,因此,你必须让它的内部。内部方法可能是组装的有用之外,因此,你会做公开。因此,所有的方法应该是公开的。如果你认为这是一个不好的说法,那么什么是你做同样的论据类,而不是方法的不同?

Your argument that nested classes are useless is also an argument that private methods are useless: a private method might be useful outside of the class, and therefore you'd have to make it internal. An internal method might be useful outside of the assembly, and therefore you'd make it public. Therefore all methods should be public. If you think that's a bad argument, then what is different about you making the same argument for classes instead of methods?

我做嵌套类所有的时间,因为我的一个助手,使课堂外没有意义的封装功能所需的位置很频繁,并且可以使用外部类的私有实现细节。比如,我写的编译器。我最近写了一个类SemanticAnalyzer,做分析树的语义分析。它的一个嵌套类是LocalScopeBuilder。在什么情况下,我需要建立一个本地范围,当我的的分析解析树的语义?决不。该类是完全语义分析器的实施方案的细节。我打算像NullableArithmeticAnalyzer和OverloadResolutionAnalyzer同时也是不是类以外的有用的名称添加更多的嵌套类,但我想封装在这些特定类的语言规则。

I make nested classes all the time because I am frequently in the position of needed to encapsulate functionality in a helper that makes no sense outside of the class, and can use private implementation details of the outer class. For example, I write compilers. I recently wrote a class SemanticAnalyzer that does semantic analysis of parse trees. One of its nested classes is LocalScopeBuilder. Under what circumstances would I need to build a local scope when I am not analyzing the semantics of a parse tree? Never. That class is entirely an implementation detail of the semantic analyzer. I plan to add more nested classes with names like NullableArithmeticAnalyzer and OverloadResolutionAnalyzer that are also not useful outside of the class, but I want to encapsulate rules of the language in those specific classes.

人们也使用嵌套类来构建之类的​​东西迭代器,或者比较 - 事情就没有任何意义了课堂之外,并通过一个众所周知的接口都暴露出来。

People also use nested classes to build things like iterators, or comparators - things that make no sense outside of the class and are exposed via a well-known interface.

我使用相当频繁的模式是具有扩展其外部类私有嵌套类:

A pattern I use quite frequently is to have private nested classes that extend their outer class:

abstract public class BankAccount
{
    private BankAccount() { }
    // Now no one else can extend BankAccount because a derived class
    // must be able to call a constructor, but all the constructors are
    // private!
    private sealed class ChequingAccount : BankAccount { ... }
    public static BankAccount MakeChequingAccount() { return new ChequingAccount(); }
    private sealed class SavingsAccount : BankAccount { ... }

和等。嵌套类的工作非常好,工厂模式。这里的BankAccount是一个工厂的各类银行账户,所有这一切都可以使用的BankAccount的私人实现细节。但是,没有第三方可以扩展的BankAccount他们自己的类型EvilBankAccount。

and so on. Nested classes work very well with the factory pattern. Here BankAccount is a factory for various types of bank account, all of which can use the private implementation details of BankAccount. But no third party can make their own type EvilBankAccount that extends BankAccount.

这篇关于是什么原因之一可能需要使用嵌套类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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