Visual Studio代码不允许我实现接口(Java)
我有一个包含两个java文件的项目。一个是带有main方法的类,另一个是带有两个方法的接口,这是在Java类中实现的,我确实重写了那里的函数 这是我在Java类中的代码:Visual Studio代码不允许我实现接口(Java),java,intellij-idea,visual-studio-code,interface,Java,Intellij Idea,Visual Studio Code,Interface,我有一个包含两个java文件的项目。一个是带有main方法的类,另一个是带有两个方法的接口,这是在Java类中实现的,我确实重写了那里的函数 这是我在Java类中的代码: public class Point implements Compare { int y; int x; public Point(int x, int y) { this.x = x; this.y = y; } public void setX (int x) { this.x = x; }
public class Point implements Compare {
int y;
int x;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public void setX (int x) {
this.x = x;
}
public void setY (int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public boolean isEqualTo(Point x) {
if ((this.x == x.getX()) && (this.y == x.getY()))
return true;
else
return false;
}
public boolean isSmallerThan(Point x) {
if (this.x < x.getX())
return true;
else if (this.y < x.getY())
return true;
else
return false;
}
public static void main(String[] args) {
System.out.println("Test");
}
当我尝试运行代码时,总是出现以下错误:
Point.java:1: error: Point is not abstract and does not override abstract method isSmallerThan() in Compare
public class Point implements Compare {
^
1 error
奇怪的是,当我用IntelliJ IDEA在project中编写代码时,同样的代码也可以工作。
我还没有在网上找到任何东西。
啊,我在macOS上工作。
希望任何人都能帮忙,为什么代码在VSC中不起作用。
谢谢事实上,这不是Visual Studio代码。这是一个Java编译器。
它告诉您“如果您声明了接口方法,然后使用此接口实现类,则必须实现或使用它们。方法签名应相同。”接口中的方法不带参数。你的类中的方法可以。因此,该类没有实现接口,因为这些方法不同。现在感谢您它的工作:-)
Point.java:1: error: Point is not abstract and does not override abstract method isSmallerThan() in Compare
public class Point implements Compare {
^
1 error