简体   繁体   English

链表中的 JAVA 类型参数

[英]JAVA Type Parameter in Linked List

Java type parameter declaration as a part of inner class. Java 类型参数声明作为内部 class 的一部分。

The question I have is on linked list.我的问题在链表上。 I am seeing the same thing in ArrayList implementation.我在ArrayList实现中看到了同样的情况。

public class LinkedList<E> implements Iterable<E> {....

private class LinkedIterator implements Iterator<E>{...} //inner class of LinkedList

private static class ListNode<E> //implemented as nested static class

Question: Why does the iterator (and constructor for LinkedList) both do not require type parameter, I think listNode requires because static class make it can not access class header of the outter class. Question: Why does the iterator (and constructor for LinkedList) both do not require type parameter, I think listNode requires because static class make it can not access class header of the outter class.

Any class definition that is:任何 class 定义是:

  • Inside another class definition.在另一个 class 定义中。
  • not marked static标记static

is an inner class.是一个内部 class。 Conceptually, the class exists only within a single instance of the outer class.从概念上讲,class 仅存在于外部 class 的单个实例中。 Effectively, it means this:实际上,这意味着:

  • There is a hidden, private, and final field of the outer type.外部类型有一个隐藏的、私有的和最终的字段。 All constructors require an instance of outer, but java syntax is special to pass it: outerRef.new Inner();所有的构造函数都需要一个外部实例,但是 java 语法是特殊的: outerRef.new Inner(); - and if you are in a context where Outer.this would be sensible, that is the default value. - 如果您处于Outer.this合理的上下文中,则这是默认值。 This field does not have a getter.该字段没有吸气剂。

  • All type params of the outer class are also available in the inner class unless shadowed.外部 class 的所有类型参数在内部 class 中也可用,除非有阴影。

So, given that private class LinkedIterator is inside class LinkedList , it is this kind of inner class, it has that hidden field, and it inherits the <E> from LinkedList<E> .因此,鉴于private class LinkedIteratorclass LinkedList内部,它就是这种内部 class,它具有隐藏字段<E>来自LinkedList<E> The Node, however, is static , therefore it does not have that field and does not inherit the <E> - instead choosing to declare it itself.然而,节点是static ,因此它没有该字段并且不继承<E> - 而是选择自己声明它。

Note that inner classes are complicated and surprising.请注意,内部类既复杂又令人惊讶。 They can for example mess up garbage collecting (because they hold a reference to their outer; that will prevent gc of the outer, for example).例如,它们可能会弄乱垃圾收集(因为它们持有对其外部的引用;例如,这将阻止外部的 gc)。 As a general rule, if you're not sure, strongly prefer static inner classes, making any inheritance of either an instance of outer or its generics explicit.作为一般规则,如果您不确定,强烈推荐 static 内部类,使外部实例或其 generics 的任何 inheritance 显式。

In other words:换句话说:

public class Outer<E> {
    public class Inner {}
}

is tricky;很棘手; don't do that unless you're sure the concept of being an inner class fully applies.不要这样做,除非您确定内部 class 的概念完全适用。 If you don't know what that means or aren't sure, write this instead:如果您不知道这意味着什么或不确定,请改写为:

public class Outer<E> {
    public static class Inner<E> {
        private final Outer<E> context;

        public Inner(Outer<E> context) {
            this.context = context;
        }
    }
}

omitting all parts you don't need, of course.当然,省略所有不需要的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM