如何修复在JAVA VS code中的实现错误,”必须实现继承的抽象类”。

huangapple go评论75阅读模式
英文:

How do I fix implementation error in JAVA VS code, "must implement the inherited abstract class"

问题

我目前正在学习接口、类和泛型。

我的当前任务是创建一个类,模拟 Java 中的 "ArrayLists"(可能是 Java 7)。

这是接口:1

我已经创建了一个类:

public class StudentArrayList implements SimpleArrayList<E> {

但是我遇到了这个错误:我遇到了这个错误

错误:StudentArrayList 类必须实现继承的抽象方法 SimpleArrayList<E>.set(int, E)Java(67109264)

我已经在类中实现了所有的函数签名,但它仍然要求我一遍又一遍地实现。我该如何解决这个问题?请告诉我。谢谢!

StudentArrayList.java:

public class StudentArrayList implements SimpleArrayList<E> {

    public static void main(String[] args) {
        System.out.println("Success");
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    // ... 其他函数实现 ...

    @Override
    public E get(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void set(int index, E element) {
        // TODO Auto-generated method stub
    }

    // ... 其他函数实现 ...
}
英文:

I am currently learning interfaces, classes and generics.

My current assignment is to create a class which mimics "ArrayLists" in Java. (probably java 7)

this is the interface

I have created a class:
public class StudentArrayList implements SimpleArrayList&lt;E&gt; {

but I am getting this error: I am getting this error

Error: The type StudentArrayList must implement the inherited abstract method SimpleArrayList&lt;E&gt;.set(int, E)Java(67109264)

I have already implemented all the function signatures in the class, but it is still me asking to implement again and again.
How do I fix this issue? please let me know.
Thanks!

StudentArrayList.java:

public class StudentArrayList implements SimpleArrayList&lt;E&gt; {

    public static void main(String[] args) {
        System.out.println(&quot;Success&quot;);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean contains(E o) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public E[] toArray() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void add(E e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void remove(E o) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean containsAll(SimpleArrayList&lt;E&gt; c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean addAll(SimpleArrayList&lt;E&gt; c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean addAll(int index, SimpleArrayList&lt;E&gt; c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean removeAll(SimpleArrayList&lt;E&gt; c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean retainAll(SimpleArrayList&lt;E&gt; c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

    @Override
    public E get(int index) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void set(int index, E element) {
        // TODO Auto-generated method stub

    }

    @Override
    public void add(int index, E element) {
        // TODO Auto-generated method stub

    }

    @Override
    public void remove(int index) {
        // TODO Auto-generated method stub

    }

    @Override
    public int indexOf(E o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public int lastIndexOf(E o) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public SimpleArrayList subList(int fromIndex, int toIndex) {
        // TODO Auto-generated method stub
        return null;
    }

}`

答案1

得分: 1

你似乎正在尝试编写一个实现通用接口的通用类。问题在于你实际上并没有将这个类声明为通用类。因此编译器认为 E 是一个类名。

原始声明:

public class StudentArrayList implements SimpleArrayList&lt;E&gt; {

应改为:

public class StudentArrayList&lt;E&gt; implements SimpleArrayList&lt;E&gt; {

StudentArrayList 后面的 &lt;E&gt;E 声明为 StudentArrayList 的类型参数。

编译错误可能是由于编译器错误地将 E 视为类名(而不是类型参数),并且无法找到任何关于它的定义。

英文:

You appear to be trying to write a generic class that implements an generic interface. The problem is that you have not actually declared the class as generic. And the compiler therefore things that E is a class name.

The declaration:

public class StudentArrayList implements SimpleArrayList&lt;E&gt; {

should be:

public class StudentArrayList&lt;E&gt; implements SimpleArrayList&lt;E&gt; {

The &lt;E&gt; after StudentArrayList declares E as a type parameter of StudentArrayList.

The compilation errors were probably a result of the compiler thinking that E was a class name (rather than a type parameter) and not being to find any definition for it.

huangapple
  • 本文由 发表于 2020年7月25日 11:05:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63083895.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定