java - 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 中不起作用。谢谢
解决方案
实际上,它不是 Visual Studio Code。它是一个 Java 编译器。它告诉你“如果你声明了接口方法,然后用这个接口实现类,你必须实现它们或使用抽象类。方法签名应该是一样的。”
推荐阅读
- python - Python中的两个字典比较
- r - 将 2015 年和 2016 年的销售额与 R 中以下数据框中的 Tier 和 Region 进行比较
- android - 从第一个活动到第二个活动的意图图像,并将文本添加到 Firebase 数据库中
- phpword - 包含文件夹中缺少文件 PhpOffice\PhpWord\TemplateProcessor.php
- jenkins - 从 Jenkins 作业运行交互式 docker 容器
- aeron - 在生产中的 Aeron 集群中设置文件同步级别 0 是否安全?
- android - 获取视图的位置并将其设置为 x,y 以用于另一个视图,但它们不匹配
- python - 我可以使用带有 dtype=object 的 numpy 数组在类实例之间共享任意类型的列表吗?
- python - pandas groupby 单列但添加多列的内容
- docker - yum 在 Centos 上安装 Docker | 需要容器-selinux libseccomp slirp4netns fuse-overlayfs 错误