java SE8 允許在接口中增加靜態方法。 理論上講,沒有任何理由認爲這個是不合法的
。只是這有違於將接口作爲抽象規範的初衷。
通常的做法都是將靜態方法放在伴隨類中。 標註庫中 你將會看到成對的接口和實用工具類,
如Collection/Conllections 或Path/Paths
可以爲Path接口增加以下方法:
1. 接口中增加靜態方法:
public interface Path{
public static Path get (String first, String ... more){
returm FileSystems .getDefault().getPath(first,more);
}
}
不在需要爲實用工具方法另外提供一個伴隨類。
默認方法
public interface Comparable<T>{
default int compareTo(T other){
//
}
}