【发布时间】:2019-05-26 04:56:57
【问题描述】:
我通过在 public static void main() 方法中实现接口 I 创建了匿名类。所以,java 8 对抽象方法 test() 的实现是由 C 类的 imple() 方法提供的。
所以,在 public static void main() 方法中,打印 _interface.getClass(),我得到了
package_path.Main$$Lambda$1/310656974 这绝对没问题。因为它打印的是匿名类名。
另外,_interface 指向堆中的一个匿名对象,因此我正在做 _interface.test();
所以,test() 方法现在的第一条语句是打印类名,
但最终打印出来的是, package_path.C (告诉我 C 是类名)。这怎么可能?不应该再次打印 package_path.Main$$Lambda$1/310656974 吗?因为“this”在测试方法中意味着匿名,对吧?
@java.lang.FunctionalInterface
interface I {
void test();
}
class C {
void imple() {
System.out.println(this.getClass());
System.out.println("Inside Implementation");
}
}
class Main {
public static void main(String[] args) {
I _interface = new C()::imple;
System.out.println(_interface.getClass());
_interface.test();
}
}
【问题讨论】:
标签: java java-8 anonymous-class functional-interface