Interface default method介紹

一、introduce interface default method

Introduce default method
Write the default method at interface
The multiply conflict resolve

interface default method/static method 

 

JDK1.8中爲什麼接口中要引入default方法?比如JDK以前的版本如JDK1.0 List接口:

public interface List<E>{
	void add(E e);
}

其他的Commons/Guavaa等第三方實現了JDK的List接口,就要重寫add方法。

jdk1.8之後在List等很多接口中都加入了stream()的獲取方法:

default Stream<E> stream();

如果stream方法不是default的話,那麼這些第三方的實現List的類統統都要加上stream()的實現,改動太大,jdk1.8爲了讓第三方的這些實現不需要改動,完美兼容,就將stream()等這些方法
設置爲default,那些第三方的實現類就不需要做任何改動,就可以使用默認的stream方法,也可以重寫它。

 

二、自己寫個簡單的含有default方法的interface

package com.cy.java8;

public class DefaultInAction {
    public static void main(String[] args) {
        A a = () -> 10;

        System.out.println(a.size());   //10
        System.out.println(a.isEmpty());//false

    }

    @FunctionalInterface
    private interface A{

        int size();

        default boolean isEmpty(){
            return size() == 0;
        }
    }
}

 

三、一個類如果實現多個接口,多個接口中含有重複名字的方法,怎麼解決衝突?

三大原則:

1.Classes always win:class的優先級是最高的。比如class C重寫了hello方法,優先級最高。
2.Otherwise, sub-interface win:if B extends A, B is more specific than A.
3.Finally, if the choice is still ambiguous, 那麼你自己就要重寫了。C implements A, B 。A和B沒有任何關係,那麼C必須重寫hello方法。

原則1對應的代碼例子:

 1 package com.cy.java8;
 2 
 3 public class DefaultInAction {
 4     public static void main(String[] args) {
 5         B c = new C();
 6         c.hello();          //C.hello
 7     }
 8 
 9     private interface A{
10         default void hello(){
11             System.out.println("A.hello");
12         }
13     }
14 
15     private interface B extends A{
16         @Override
17         default void hello() {
18             System.out.println("B.hello");
19         }
20     }
21 
22     private static class C implements A, B{
23         @Override
24         public void hello() {
25             System.out.println("C.hello");
26         }
27     }
28 }

原則2對應的代碼例子:

 1 package com.cy.java8;
 2 
 3 public class DefaultInAction {
 4     public static void main(String[] args) {
 5         A c = new C();
 6         c.hello();          //B.hello
 7     }
 8 
 9     private interface A{
10         default void hello(){
11             System.out.println("A.hello");
12         }
13     }
14 
15     private interface B extends A{
16         @Override
17         default void hello() {
18             System.out.println("B.hello");
19         }
20     }
21 
22     private static class C implements A, B{
23 
24     }
25 }
View Code

原則3對應的代碼例子:

 1 package com.cy.java8;
 2 
 3 public class DefaultInAction {
 4     public static void main(String[] args) {
 5         C c = new C();
 6         c.hello();      //C.hello
 7     }
 8 
 9     private interface A{
10         default void hello(){
11             System.out.println("A.hello");
12         }
13     }
14 
15     private interface B{
16         default void hello() {
17             System.out.println("B.hello");
18         }
19     }
20 
21     private static class C implements A, B{
22         @Override
23         public void hello() {
24             //A.super.hello();
25             //B.super.hello();
26             System.out.println("C.hello");
27         }
28     }
29 }
View Code

 

  

 

 

 

----

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章