接口里面可以有方法体?
答案是可以有的。因为接口可以定义静态方法。
有点疏忽,一直都以为,接口里面的成员只能有:
成员常量
和
抽象方法
。。。。
接口里面还可以定义哪些成员????
/*下面是Map接口的内部接口Entry的源码。。。也就是我们取Map集合经常用到的Map.Entry*/
- interface Entry<K,V> {
-
- K getKey();
-
- V getValue();
-
- V setValue(V value);
-
- boolean equals(Object o);
-
- int hashCode();
-
- /*很奇怪这些静态方法在api文档里找不到?这是什么情况???????*/
- public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
- return (Comparator<Map.Entry<K, V>> & Serializable)
- (c1, c2) -> c1.getKey().compareTo(c2.getKey());
- }
-
- public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
- return (Comparator<Map.Entry<K, V>> & Serializable)
- (c1, c2) -> c1.getValue().compareTo(c2.getValue());
- }
-
- public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
- Objects.requireNonNull(cmp);
- return (Comparator<Map.Entry<K, V>> & Serializable)
- (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
- }
-
- public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
- Objects.requireNonNull(cmp);
- return (Comparator<Map.Entry<K, V>> & Serializable)
- (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
- }
- }
复制代码
|
|