简体   繁体   English

内部类泛型-不兼容的类型

[英]Inner class generics - Incompatible types

i just can't seem to get why i get that these are incompatible types, is it just bc they are on different class levels? 我只是似乎无法理解为什么我得到了这些不兼容的类型,难道只是因为它们处于不同的类级别? I just can't quite get why. 我只是不明白为什么。

public class PrependLinearListImpl<T>{
    private Node<T> first;

    private class Node<T> {
        private T head;
        private Node<T> tail;
        Node(T head) {
            this.head = head;
            this.tail = first;
            first = this;
        }
    }

}

It's because PrependLinearListImpl<T>.Node already inherits the generic argument from its outer class. 这是因为PrependLinearListImpl<T>.Node已经从其外部类继承了通用参数。 It's not necessary to redefine the generic component. 不必重新定义通用组件。

The following should work as-is: 以下应按原样工作:

public class PrependLinearListImpl<T>{
    private Node first;

    private class Node {
        private T head;
        private Node tail;
        Node(T head) {
            this.head = head;
            this.tail = first;
            first = this;
        }
    }

}

If Node were static , then it would be necessary to provide its own generic parameter. 如果Nodestatic ,则有必要提供自己的通用参数。

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

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