这个简单的例子演示了这个问题:
public class Main {
interface Person {
default int amountOfHands() {
return 2;
}
}
public static class BasicPerson implements Person {
int numberOfFaces() {
return 1;
}
}
public static void main(String[] args) {
System.out.println("Put a breakpoint here");
}
}
我在 IntelliJ IDEA 中以调试模式运行此代码,并将两个手表放在 main 方法中:
new BasicPerson().amountOfHands();
new BasicPerson().numberOfFaces();
这两种方法都应该返回一个原始 int,但是只有第二个 watch(类方法)显示一个原始 int,而第一个(默认接口方法)显示一个装箱的 Integer 对象。
这是为什么?那是一个错误吗?