java Java内部类的用途是什么?嵌套类和内部类是否相同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2941682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:34:16  来源:igfitidea点击:

What are the uses of inner classes in Java? Are nested classes and inner classes the same?

javainner-classes

提问by billu

Possible Duplicate:
Java inner class and static nested class

可能的重复:
Java 内部类和静态嵌套类

What are the uses of inner classes in Java? Are nested classes and inner classes the same?

Java内部类的用途是什么?嵌套类和内部类是否相同?

采纳答案by polygenelubricants

No, they're not the same: an inner class is non-static.

不,它们不一样:内部类是非static.

JLS 8.1.3 Inner Classes and Enclosing Instances

An innerclass is a nested class that is not explicitly or implicitly declared static.

JLS 8.1.3 内部类和封闭实例

一个内部类是不明确或隐含声明为静态的嵌套类。

Consult also the following diagram from Joe Darcy:

另请参阅 Joe Darcy 的下图:

Joseph D. Darcy's Oracle Weblog - Nested, Inner, Member, and Top-Level Classes

alt text

Joseph D. Darcy 的 Oracle 博客 - 嵌套类、内部类、成员类和顶级类

替代文字



Related questions

相关问题

回答by ewernli

The difference is well addressed by the other answer. Regarding their usage/relevance, here is my view:

另一个答案很好地解决了差异。关于它们的用法/相关性,这是我的观点:

Anonymous class: handy for productivity

匿名课程:提高生产力

They are handy to implement callbacks easily, without the burden to create new namedclass.

它们可以方便地轻松实现回调,而无需创建新的命名类。

button.addActionListener(
      new ActionListener() {
         public void actionPerformed( ActionEvent e ) {
              frame.dispose();
         }
      }
);

They are also handy for threading (e.g. anonymous Runnable) and a few other similar pattern.

它们对于线程(例如匿名Runnable)和其他一些类似的模式也很方便。

Static nested class: handy for encapsulation

静态嵌套类:便于封装

Static nested classes are essentially like regular classes, except that their name is OuterClass.StaticNestedClassand you can play with modifier. So it provided some form of encapsulation that can not exactly be achieved with top-level classes.

静态嵌套类本质上与常规类类似,不同之处在于它们的名称是OuterClass.StaticNestedClass并且您可以使用修饰符。所以它提供了某种形式的封装,这是顶级类无法完全实现的。

Think for instance of a LinkedListfor which you would like the class Node(used only internally) to not be visible in the package view. Make it a static nested class so that it's fully internal to the LinkedList.

例如LinkedList,您希望类Node(仅在内部使用)在包视图中不可见。使它成为一个静态嵌套类,以便它完全在LinkedList.

Inner class: handy for ownershipand encapsulation

内部类:便于所有权封装

An instanceof an inner class has access to the field of its enclosing class instance. Think again of the linked list and imagine Nodeis an inner class:

内部类的实例可以访问其封闭类实例的字段。再想想链表,想象一下Node是一个内部类:

public class LinkedList {
  private Node root = null;

  public class Node {
    private Object obj;
    private Node next;

    ...

    public void setAsRoot() {
       root = this;
    }
  }

  public Node getRoot() {
    return root;
  }

  public void setRoot( Node node ) {
    root = node;
  }

}

Each Nodeinstance belonging to a LinkedListcan access it directly. There is an implicit ownership relationshipbetween the list and its nodes; the list owns its nodes. The same ownership relationship would require extra code if implemented with regular classes.

Node属于 a 的每个实例LinkedList都可以直接访问它。列表与其节点之间存在隐式所有权关系;该列表拥有其节点。如果使用常规类实现,相同的所有权关系将需要额外的代码。

See Achieve better Java code with inner and anonymous classes

请参阅使用内部类和匿名类实现更好的 Java 代码