简体   繁体   English

访问外部类引用时Java内部类出错

[英]Error with java inner class when access outer class reference

I am implementing Trie data structure. 我正在实现Trie数据结构。 When I use the outer class reference, I am getting the below error in Eclipse IDE. 当使用外部类引用时,在Eclipse IDE中出现以下错误。

The method compare(capture#1-of ? super E, capture#1-of ? super E) in the type Comparator is not applicable for the arguments (E, E) 比较器类型中的方法compare(capture#1-of?super E,capture#1-of?super E)不适用于参数(E,E)

public class Trie<E> implements Collection<E> {

    private Comparator<? super E> comparator;

    private class Entry<E> {        
        private Entry<E> parent;
        private Set<Entry<E>> children;

        private E data;

        Entry(Entry<E> parent, E data){
            this.parent = parent;
            this.data = data;

            parent.addChild(this);
        }

        boolean addChild(Entry<E> child){
            if (children == null)
                children = new TreeSet<Entry<E>>(
                        new Comparator<Entry<E>>() {                            
                            public int compare(Entry<E> o1, Entry<E> o2) {
                                return Trie.this.comparator.compare(o1.data, o2.data);
                            };
                        }
                        );

            return children.add(child);
        }

        boolean removeChild(Entry<E> child){
            boolean result = false;
            if (children != null && children.contains(child)) {
                result = children.remove(child);

                if (children.size() == 0)
                    children = null;
            }

            return result;          
        }
    }
}

How to resolve? 怎么解决?

在Eclipse中显示错误

You omitted one crucial warning in your post: The type parameter E is hiding the type E on the line private class Entry<E> . 您在帖子中省略了一个重要警告: The type parameter E is hiding the type Eprivate class Entry<E>The type parameter E is hiding the type E

There is no relation between the type parameter E in inner class Entry and the one in outer class Trie . 内部类Entry的类型参数E和外部类Trie的类型参数E之间没有关系。 You can change E to F in Entry and you will get exactly the same error. 您可以在Entry中将E更改为F ,并且会得到完全相同的错误。

However if you don't redefine <E> in the class declaration Entry it will work because the inner class has access to the type parameter of the outer class (initially I forgot that) 但是,如果您没有在类声明Entry 重新定义<E> ,它将起作用,因为内部类可以访问外部类的类型参数(起初我忘记了)

What you need to do is this: replace every occurrence of Entry<E> with just Entry . 你需要做的是这样的: 替代的每次出现Entry<E>只需Entry

public class Trie<E> implements Collection<E> {

    private Comparator<? super E> comparator;

    private class Entry {
        private Entry parent;
        private Set<Entry> children;

        private E data;

        Entry(Entry parent, E data) {
            this.parent = parent;
            this.data = data;

            parent.addChild(this);
        }

        boolean addChild(Entry child) {
            if (children == null)
                children = new TreeSet<Entry>(new Comparator<Entry>() {
                    public int compare(Entry o1, Entry o2) {
                        return Trie.this.comparator.compare(o1.data, o2.data);
                    };
                });

            return children.add(child);
        }

        boolean removeChild(Entry child) {
            boolean result = false;
            if (children != null && children.contains(child)) {
                result = children.remove(child);
                if (children.size() == 0)
                    children = null;
            }
            return result;
        }
    }
}

That will make your problem go away. 那将使您的问题消失。

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

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