【问题标题】:Error with java inner class when access outer class reference访问外部类引用时java内部类出错
【发布时间】:2014-07-23 07:29:57
【问题描述】:

我正在实现 Trie 数据结构。当我使用外部类引用时,在 Eclipse IDE 中出现以下错误。

方法 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;          
        }
    }
}

如何解决?

【问题讨论】:

  • 是的,试过了,得到这个错误“Comparator 类型中的方法 compare(E, E) 不适用于参数 (E, E)”
  • 能否请您发布您的代码而不是 Image ?以便人们可以尝试尽快为您提供解决方案。你想要你的比较器像private Comparator&lt;? extends E&gt; comparator ??
  • 您内部班级中的E 阴影(与外部班级中的E 不同);它们指的是不同的不相关类型。还将您的代码发布为代码,而不是屏幕截图;这样人们就可以在答案中更正您的代码(他们不会再次输入)
  • Comparator&lt;E&gt; != Comparator&lt;Entry&lt;E&gt;&gt;。一旦你有一个 E 一旦你有一个 Entry.

标签: java eclipse generics inner-classes


【解决方案1】:

您在帖子中遗漏了一个重要警告:The type parameter E is hiding the type E 在线 private class Entry&lt;E&gt;

内部类Entry中的类型参数E与外部类Trie中的类型参数没有关系。您可以将Entry 中的E 更改为F,您将得到完全相同的错误。

但是,如果您不在类声明Entry 中重新定义&lt;E&gt;,它将起作用,因为内部类可以访问外部类的类型参数(最初我忘记了)

您需要做的是:将每次出现的Entry&lt;E&gt; 替换为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;
        }
    }
}

这会让你的问题消失。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 2014-02-11
    • 2011-01-02
    相关资源
    最近更新 更多