A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 354620815 中级黑马   /  2014-10-13 17:18  /  1188 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

接口里面可以有方法体?
答案是可以有的。因为接口可以定义静态方法。
有点疏忽,一直都以为,接口里面的成员只能有:
成员常量

抽象方法
。。。。
接口里面还可以定义哪些成员????

/*下面是Map接口的内部接口Entry的源码。。。也就是我们取Map集合经常用到的Map.Entry*/
  1. interface Entry<K,V> {
  2.    
  3.     K getKey();
  4.    
  5.     V getValue();
  6.    
  7.     V setValue(V value);
  8.    
  9.     boolean equals(Object o);
  10.   
  11.     int hashCode();
  12.    
  13. /*很奇怪这些静态方法在api文档里找不到?这是什么情况???????*/
  14.     public static <K extends Comparable<? super K>, V> Comparator<Map.Entry<K,V>> comparingByKey() {
  15.         return (Comparator<Map.Entry<K, V>> & Serializable)
  16.             (c1, c2) -> c1.getKey().compareTo(c2.getKey());
  17.     }
  18.    
  19.     public static <K, V extends Comparable<? super V>> Comparator<Map.Entry<K,V>> comparingByValue() {
  20.         return (Comparator<Map.Entry<K, V>> & Serializable)
  21.             (c1, c2) -> c1.getValue().compareTo(c2.getValue());
  22.     }
  23.   
  24.     public static <K, V> Comparator<Map.Entry<K, V>> comparingByKey(Comparator<? super K> cmp) {
  25.         Objects.requireNonNull(cmp);
  26.         return (Comparator<Map.Entry<K, V>> & Serializable)
  27.             (c1, c2) -> cmp.compare(c1.getKey(), c2.getKey());
  28.     }
  29.    
  30.     public static <K, V> Comparator<Map.Entry<K, V>> comparingByValue(Comparator<? super V> cmp) {
  31.         Objects.requireNonNull(cmp);
  32.         return (Comparator<Map.Entry<K, V>> & Serializable)
  33.             (c1, c2) -> cmp.compare(c1.getValue(), c2.getValue());
  34.     }
  35. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马