Interface default method介绍

1、introduce interface default methodjava

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

interface default method/static method spa

 

JDK1.8中为何接口中要引入default方法?好比JDK之前的版本如JDK1.0 List接口:code

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

其余的Commons/Guavaa等第三方实现了JDK的List接口,就要重写add方法。blog

jdk1.8以后在List等不少接口中都加入了stream()的获取方法:接口

default Stream<E> stream();

若是stream方法不是default的话,那么这些第三方的实现List的类通通都要加上stream()的实现,改动太大,jdk1.8为了让第三方的这些实现不须要改动,完美兼容,就将stream()等这些方法
设置为default,那些第三方的实现类就不须要作任何改动,就能够使用默认的stream方法,也能够重写它。ip

 

2、本身写个简单的含有default方法的interfaceci

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;
        }
    }
}

 

3、一个类若是实现多个接口,多个接口中含有重复名字的方法,怎么解决冲突?it

三大原则:io

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

 

  

 

 

 

----

相关文章
相关标签/搜索