内部类的目的是什么 [英] What are the purposes of inner classes

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

问题描述

我正在检查Java中内部类的概念。因为我已经理解和应用了java内部类有一个链接或访问其外部/封闭类的方法和字段。



我的问题: p>


  1. 什么时候应该创建或定义内部类?

  2. 是被视为辅助类的内部类?

  3. 您要创建内部类的指标是什么,目的?
  4. 内部类最适合用于逻辑分组在一个地方使用的类。例如,如果你想创建只由封装类使用的类,那么为它创建一个单独的文件没有意义。您可以将其添加为内部类



    根据 javadoc


    使用嵌套类的令人信服的原因包括:




    • 这是一种逻辑分组只在一个
      位置使用的类的方法。

    • 它增加封装。

    • 它可以带来更易读和可维护的代码。



    I am reviewing the concept of inner classes in java. so far from what I've understood and applied java inner classes has a link or access to the methods and fields of its outer/ enclosing class.

    My Question:

    1. When should create or define an inner class?
    2. are inner classes considered to be called as "Helper classes" ?
    3. What are the indicators for you to make an inner class and what's their other purpose?

    解决方案

    Inner classes are best for the purpose of logically grouping classes that are used in one-place. For example, if you want to create class which is used by ONLY enclosing class, then it doesn't make sense to create a separate file for that. Instead you can add it as "inner class"

    As per javadoc:

    Compelling reasons for using nested classes include the following:

    • It is a way of logically grouping classes that are only used in one place.
    • It increases encapsulation.
    • It can lead to more readable and maintainable code.

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

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