什么时候使用嵌套Java类真正有用? [英] When is really useful to use a nested Java class?

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

问题描述

能否给我一个具体的例子,其中嵌套的Java类很有用? 我正在研究它,我理解它是如何工作的,但是我无法想象真正需要使用它的真实情况.

非常感谢您

马可

解决方案

何时使用嵌套类的最简洁的摘要是:何时该类在逻辑上是外部类的API的一部分,或者何时进行封装外部类的特定行为.

例如,Map.Entry:它是映射中的一个条目.从逻辑上讲,它是Map API的一部分,因此将其放入Map中是很有意义的.

另一个常见的示例是构建器类:您使用Builder来制作东西,因此将其放入该东西中是很有意义的.

这些是仅在使用外部类的情况下才真正使用的类:有时您可能单独将Map.Entry用作某种配对类.有时您可能想单独使用Builder,例如,将方法放入该构建器的方法的参数,但实际上并没有真正进行构建.这些情况可能也很少与也使用外部类的代码相距很远:您仍在使用外部类的API的某些方面,因此它们在逻辑上仍然属于该API.

您可以将此类放在主"类的顶层.您可能不希望这样做的原因有几个:

  • 它使名称空间混乱.如果您有类FooBarBaz等,则在顶层具有FooBuilderBarBuilderBazBuilder只会使您很难看到有用的"顶层类.
    • Google的协议缓冲区会为每种消息类型生成一个Builder类.考虑到Google代码中使用的协议缓冲区的数量,这种混乱情况将非常繁琐.
  • 嵌套类可以访问外部类的私有字段,这可以帮助您做某些事情而不必不必要地暴露这些字段.
  • 您可以通过限定外部名称来引用嵌套类,例如Outer.Builder,而不必显式地import some.pkg.OuterBuilder 以及 import some.pkg.Outer.诚然,我并不真正在乎进口的数量,因为我只是将它们保留在intellij中.

您还可以使用它们在类中封装内部逻辑或中间状态:例如,当我发现自己在类的私有方法之间传递相同的N个参数时,我喜欢定义私有嵌套类.这些是您不希望班级以外的人关心的事情;这只是在类内部方便.

Could you give me a concrete example where a nested java class is useful? I am studying it, I understand how it works but I can not imagine a real situation where its use is really necessary.

Thank you so much

Marco

解决方案

The most concise summary of when to use a nested class is: when that class is logically part of the outer class' API, or when it encapsulates behavior specific to the outer class.

For example, Map.Entry: it's an entry in a mapping. It's logically part of Map's API, so it makes sense to put it inside Map.

Another common example is Builder classes: you use a Builder to make a thing, so it makes sense to put it inside that thing.

These are classes that you only really use in the context of using the outer classes: sometimes you might use a Map.Entry by itself as some sort of pair class; and sometimes you might want to use a Builder by itself, for example as a parameter to a method which puts things into that builder, but doesn't actually do the building itself. These cases are probably rarely far from code that also uses the outer class too: you're still using some aspect the outer class' API, so they do still logically belong to that API.

You could put such classes at the top level, alongside the "main" class. There are a few reasons why you might not want to do this:

  • It clutters the namespace. If you've got classes Foo, Bar, Baz etc, having FooBuilder, BarBuilder, BazBuilder at the top level just makes it harder to see the "useful" top level classes.
    • Google's protocol buffers generate a Builder class for every message type. Given the number of protocol buffers used in Google code, this clutter would be far too cumbersome.
  • Nested classes have access to private fields of the outer class, which may help you do certain things without unnecessarily exposing those fields.
  • You can refer to nested classes by qualifying the outer name, e.g. Outer.Builder, rather than having to explicitly import some.pkg.OuterBuilder as well as import some.pkg.Outer. Admittedly, I don't really care about the number of imports, since I just keep them collapsed in intellij.

You can also use them to encapsulate internal logic or intermediate state in the class: for example, I like to define private nested classes when I find myself passing around the same N parameters between private methods in a class. This is stuff that you don't want people outside your class to care about; it's merely convenient inside the class.

这篇关于什么时候使用嵌套Java类真正有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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