英文:
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)
I have created a class:
public class StudentArrayList implements SimpleArrayList<E> {
but I am getting this error: I am getting this error
Error: The type StudentArrayList must implement the inherited abstract method SimpleArrayList<E>.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<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 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<E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(SimpleArrayList<E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(int index, SimpleArrayList<E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(SimpleArrayList<E> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(SimpleArrayList<E> 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<E> {
应改为:
public class StudentArrayList<E> implements SimpleArrayList<E> {
StudentArrayList
后面的 <E>
将 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<E> {
should be:
public class StudentArrayList<E> implements SimpleArrayList<E> {
The <E>
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论