【发布时间】:2015-05-04 17:44:08
【问题描述】:
我正在使用 IntelliJ IDEA 进行编码。在调试我的应用程序时,我无法在 Watches 中使用一些默认方法实现。
这是一个精简的例子:
public class Friendship {
interface Friend {
default void sayHiTo(Friend friend) {
System.out.println("Hi, " + friend.hashCode());
}
default int amountOfHands() {
return 2;
}
}
public static class BasicFriend implements Friend {
int numberOfFaces() {
return 1;
}
}
public static void main(String[] args) {
System.out.println("Put a breakpoint here");
}
}
在main()方法中我放了一个断点并设置了三个手表:
// Default interface method with dependency
new BasicFriend().sayHiTo(new BasicFriend())
// Default interface method without dependency
new BasicFriend().amountOfHands()
// Class method
new BasicFriend().numberOfFaces()
第一只手表抛出 NoSuchMethodException 抱怨方法 Friendship$BasicFriend.sayHiTo() 不存在。
第二个手表运行成功,但奇怪的是它报告了一个装箱的对象
{java.lang.Integer@537} "2" 而不仅仅是原始的2。
正如预期的那样,第三个手表报告了一个原始 1。
为什么第一只手表不工作?这是一个错误吗?这实际上与IDE有关吗?是因为默认方法的一些概念缺陷吗?它应该首先按照我的意愿工作吗?第二只手表的奇怪结果是否与第一只手表的问题有某种关系?
【问题讨论】:
-
重读您的问题后,除了您的第一块手表外,我得到了相同的结果。在您的第一块手表中,我收到
java.lang.ClassCastException : com.sun.tools.jdi.InterfaceTypeImpl cannot be cast to com.sun.tools.jdi.ClassTypeImpl。我猜这只是一个 IntelliJ 编译器错误。我在 Ubuntu 上使用 IntelliJ 14.1.2。
标签: debugging intellij-idea interface java-8 default-method