简体   繁体   English

错误:方法未覆盖或从超类型实现方法

[英]error: method does not override or implement a method from a supertype

Building queue implementation based on linked list. 基于链表的构建队列实现。 Cannot run application because of the two errors: 由于出现两个错误,因此无法运行应用程序:

public class Queue<Integer> implements Iterable<Integer> {
    ...
    public Iterator<Integer> iterator()  {
            return new ListIterator(first);
        }
    private class ListIterator<Integer> implements Iterator<Integer> {// error #1
            private Node<Integer> current;

            public ListIterator(Node<Integer> first) {
                current = first;
            }

            public boolean hasNext(){ return current != null;                   }
            public void remove()    { throw new UnsupportedOperationException();}

            public int next() { // error #2
                if (!hasNext()) throw new NoSuchElementException();
                int item = current.item;
                current = current.next;
                return item;
            }
        }    
     }

error #1: error: Queue.ListIterator is not abstract and does not override abstract method next() in Iterator where Integer is a type-variable: Integer extends Object declared in class Queue.ListIterator 错误#1:错误:Queue.ListIterator不是抽象的,并且不覆盖Iterator中Integer是类型变量的抽象方法next():Integer扩展了在Queue.ListIterator类中声明的对象

error #2: error: next() in Queue.ListIterator cannot implement next() in Iterator return type int is not compatible with Integer where E,Integer are type-variables: E extends Object declared in interface Iterator Integer extends Object declared in class Queue.ListIterator 错误#2:错误:Queue.ListIterator中的next()无法在Iterator中实现next()返回类型int与Integer不兼容,其中E,Integer是类型变量:E扩展了在接口Iterator中声明的对象Integer扩展了在类中声明的对象Queue.ListIterator

How to get it working? 如何使其运作?

Boxing and unboxing in Java simplify code in many places, but method return types is not one of them. Java中的装箱和拆箱在许多地方都简化了代码,但是方法返回类型不是其中之一。 The next method must return an Integer , not an int . next方法必须返回Integer而不是int It must match the generic type parameter exactly. 它必须与通用类型参数完全匹配。

public Integer next()

Second, you've declared a generic type parameter Integer in your Queue and ListIterator classes that has nothing to do with java.lang.Integer . 其次,您在QueueListIterator类中声明了一个通用类型参数Integer ,它与java.lang.Integer无关。 Remove it: 去掉它:

//              here
public class Queue implements Iterable<Integer> {

and

//                      here
private class ListIterator implements Iterator<Integer> {

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

相关问题 方法不会从父类型@override覆盖或实现方法编译错误 - method does not override or implement a method from a supertype @override compile error 错误:(124,9)错误:方法未覆盖或从超类型实现方法 - Error:(124, 9) error: method does not override or implement a method from a supertype 方法不会覆盖或实现超类型的方法 - method does not override or implement a method from a supertype 方法不会覆盖或实现超类型的方法 - method does not override or implement a method from a supertype Java“方法没有覆盖或实现来自超类型的方法”错误 - Java "method does not override or implement a method from a supertype" error React Native - 错误:方法不会覆盖或实现超类型的方法 - React Native - error: method does not override or implement a method from a supertype Java错误:方法未从超类型重写或实现方法 - java error: method does not override or implement a method from a supertype 错误:方法onEnable不会覆盖或实现超类型的方法 - Error: method onEnable does not override or implement a method from a supertype 错误:方法未覆盖或从超类型OnCreateOptionsMenu实现方法 - error: method does not override or implement a method from a supertype OnCreateOptionsMenu 方法不会从超类型错误中覆盖或实现方法 - method does not override or implement a method from a supertype error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM