我认为我对Java中的接口引入默认方法感到有些困惑.据我了解,其想法是可以在不破坏现有代码的情况下将默认方法引入现有接口.
如果我使用非抽象类实现接口,我(当然)必须定义接口中所有抽象方法的实现.如果接口定义了默认方法,我将继承该方法的实现.
如果我实现两个接口,我显然必须实现两个接口中定义的抽象方法的并集.我继承了所有默认方法的实现; 但是,如果两个接口中的默认方法之间发生冲突,我必须在我的实现类中重写该方法.
这听起来不错,但是下面的场景呢?
假设有一个接口:
package com.example ;
/**
* Version 1.0
*/
public interface A {
public void foo() ;
/**
* The answer to life, the universe, and everything.
*/
public default int getAnswer() { return 42 ;}
}
Run Code Online (Sandbox Code Playgroud)
和第二个界面
package com.acme ;
/**
* Version 1.0
*/
public interface B {
public void bar() ;
}
Run Code Online (Sandbox Code Playgroud)
所以我可以写下面的内容:
package com.mycompany ;
public class C implements com.example.A, com.acme.B {
@Override
public void foo() {
System.out.println("foo");
}
@Override
public …Run Code Online (Sandbox Code Playgroud) Java8在我的JPA EclipseLink 2.5.2环境中继续做一些奇怪的事情.我不得不删除问题/sf/ask/1876432841/ 昨天,因为在这种情况下的排序受到奇怪的JPA行为的影响 - 我通过强迫在进行最终排序之前的第一个排序步骤.
仍然在Java 8中使用JPA Eclipselink 2.5.2,以下代码有时不会在我的环境中排序(Linux,MacOSX,两者都使用build 1.8.0_25-b17).它在JDK 1.7环境中按预期工作.
public List<Document> getDocumentsByModificationDate() {
List<Document> docs=this.getDocuments();
LOGGER.log(Level.INFO,"sorting "+docs.size()+" by modification date");
Comparator<Document> comparator=new ByModificationComparator();
Collections.sort(docs,comparator);
return docs;
}
Run Code Online (Sandbox Code Playgroud)
从JUnit测试调用时,上述函数正常工作.在生产环境中进行debbuging时,我会得到一个日志条目:
INFORMATION: sorting 34 by modification date
Run Code Online (Sandbox Code Playgroud)
但是在TimSort中,nRemaining <2的返回语句被命中 - 因此不会发生排序.JPA提供的IndirectList(请参阅jpa返回的集合?)被认为是空的.
static <T> void sort(T[] a, int lo, int hi, Comparator<? super T> c,
T[] work, int workBase, int workLen) {
assert c != null && a != null && lo >= 0 && lo <= hi …Run Code Online (Sandbox Code Playgroud)