为什么要使用嵌套类? [英] Why Use Nested Classes?

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

问题描述

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



这个吸引力越少越好而不是将嵌套类放在自己的文件中,并通过构造函数传递参数。

解决方案

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


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


  2. 它增加了封装


  3. 嵌套类可以导致更多的可读性和可维护性代码


  4. 父类连接的子进程更简单,因为可视化地说明每个类的变量和方法。



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. It is a way of logically grouping classes that are only used in one place.

  2. It increases encapsulation.

  3. Nested classes can lead to more readable and maintainable code.

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

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

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