我为什么要使用嵌套类? [英] Why should I use nested classes?

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

问题描述

什么时候嵌套类是可行的?
我看到的最常见的优势是共享范围(跨类使用变量)。

When is it feasible to nest classes? The most common advantage of it that I see is "shared scope" (use of variables across classes).

这是不那么有吸引力/不是最佳实践而不只是将嵌套类放在它自己的文件中,并通过构造函数传递参数?

Is this less attractive/less a best practice than just putting the nested class in it's own file, and passing the arguments through the Constructor?

推荐答案

使用的原因有几个嵌套类,其中包括:

There are several reasons for using nested classes, among them:


  1. 这是一种仅在一个地方使用的逻辑分组类的方式。

增加封装

嵌套类可以带来更多可读性和可维护性代码

子类到父类的连接比较简单,因为它直观地说明了每个类的变量和方法。

Child to parent class connection is simpler as it visually illustrates the variables and methods of each class.

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

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