相关疑难解决方法(0)

JDK 8中的默认值是Java中的多重继承形式吗?

JDK 8中的新功能允许您在保留二进制兼容性的同时添加到现有接口.

语法就像

public interface SomeInterface() {
  void existingInterface();
  void newInterface() default SomeClass.defaultImplementation;
}
Run Code Online (Sandbox Code Playgroud)

这种方式对于所有现有的实现,SomeInterface当它们升级到这个新版本时,它们并不会突然编译错误newInterface().

虽然这很简洁,但当你实现两个接口时会发生什么呢?两个接口都添加了一个你没有实现的新默认方法?让我举个例子来解释一下.

public interface Attendance {
   boolean present() default DefaultAttendance.present;
}

public interface Timeline {
   boolean present() default DefaultTimeline.present;
}

public class TimeTravelingStudent implements Attendance, Timeline {

}

// which code gets called?
new TimeTravelingStudent().present();
Run Code Online (Sandbox Code Playgroud)

这被定义为JDK 8的一部分了吗?

我发现Java神在这里谈论类似的东西http://cs.oswego.edu/pipermail/lambda-lib/2011-February/000068.html,但它是私人邮件列表的一部分,我不能直接问他们.

有关如何在JDK 8中使用默认值以及扩展Collection接口以支持lambdas的更多详细信息,请参阅此处:https: //oracleus.wingateweb.com/published/oracleus2011/sessions/25066/25066_Cho223662.pdf

java closures interface multiple-inheritance java-8

80
推荐指数
5
解决办法
2万
查看次数

标签 统计

closures ×1

interface ×1

java ×1

java-8 ×1

multiple-inheritance ×1