首页 > 解决方案 > Visual Studio Code 不允许我实现我的接口(Java)

问题描述

我有一个包含两个 java 文件的项目。一个是带有主方法的类,另一个是带有两个方法的接口,它是在 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 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");
}

}

这是我的界面中的代码:

public interface Compare {

public boolean isEqualTo();
public boolean isSmallerThan();
}

当我尝试运行代码时,总是出现以下错误:

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 的项目中编写它时,相同的代码可以工作。我还没有在互联网上找到任何东西。啊,我在 macOS 上工作。希望任何人都可以提供帮助,为什么代码在 VSC 中不起作用。谢谢

标签: javaintellij-ideavisual-studio-codeinterface

解决方案


实际上,它不是 Visual Studio Code。它是一个 Java 编译器。它告诉你“如果你声明了接口方法,然后用这个接口实现类,你必须实现它们或使用抽象类。方法签名应该是一样的。”


推荐阅读