一、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; } } }
三、
----