【问题标题】:Visual Studio Code does not allow me to implement my interface(Java)Visual Studio Code 不允许我实现我的接口(Java)
【发布时间】:2021-02-22 20:45:36
【问题描述】:

我有一个包含两个 java 文件的项目。一个是带有 main-method 的类,另一个是带有两个方法的接口,它是在 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 中不起作用。 谢谢

【问题讨论】:

  • 你接口中的方法不带参数。你类中的方法可以。因此该类没有实现接口,因为这些方法不一样。
  • 谢谢你现在它的工作:-)

标签: java intellij-idea visual-studio-code interface


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-19
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 2021-08-02
    相关资源
    最近更新 更多