JAVA8-default、static
default方法
1.8之前出现的问题:开发了一个接口,后面实现该接口的实现类,都需要实现接口中定义的方法。1.8之后接口中使用default定义新增的方法后,其实现类可以不用实现该方法。
例如Collection接口中新增了对stream的操作方法:stream()
public interface Collection<E> extends Iterable<E> {    
    default Stream<E> stream() {
        return StreamSupport.stream(spliterator(), false);
    }
}其实现类就可以不用再实现stream()方法,这样对于版本兼容就比较好了。
static 定义的方法
可以通过接口直接调用该方法,例如:
public interface Function<T, R> {
    static <T> Function<T, T> identity() {
        return t -> t;
    }
}注意:接口中有default定义的方法实现了,但并不是抽象类。
接口:可以被多个类实现。
抽象类:继承了一个抽象类,就不能再继承另一个抽象类,因为类是单继承。
实列
自己用default实现一个接口
package com.example.study.java8.method;
/**
 * default使用
 */
public class DefaultAction {
    public static void main(String[] args) {
        A a = ()->10;
        System.out.println(a.size());
        System.out.println(a.isEmpty());
    }
    public interface A{
        int size();
        //判断容器是否为空
        default boolean isEmpty(){
            return size()==0;
        }
    }
}输出结果:
10
false问题思考:当接口中定义default方法后,被多个类实现会不会出现冲突?
问题思考,代码示例:
package com.example.study.java8.method;
public class DefaultActon2 {
    public void confuse(Object obj){
        System.out.println("Object");
    }
    public void confuse(int[] i){
        System.out.println("int[]");
    }
    public static void main(String[] args) {
        DefaultActon2 action = new DefaultActon2();
        action.confuse(null);
        int [] arr = null;
        Object obj = arr;
        action.confuse(obj);
    }
}输出结果:
int[]
Object思考:为什么传null,就是调用的int[]数组的方法,而下面的数组指向Object后,调用的时第一个object方法?
因为当传null的时候,会调用声明更具体的方法,如上面的int[] i,明确定义了是一个int数组,而Object不是特别具体。
Default方法解决多重继承冲突的三大原则
1、类的优先级最高
2、上一级接口,不管上一级接口有多少个类
3、如果混淆了,就必须重写。
类的优先级最高,示例:
package com.example.study.java8.method;
/**
 * 1、类的优先级最高
 * 2、上一级接口,不管上一级接口有多少个类
 */
public class DefaultAction3 {
    public static void main(String[] args) {
        A c = new C();
        c.hello();
    }
    public interface A{
        default void hello(){
            System.out.println("A-Hello");
        }
    }
    public interface B extends A{
        @Override
        default void hello(){
            System.out.println("B-Hello");
        }
    }
    public static class C implements B , A{
        @Override
        public void hello() {
            System.out.println("C-Hello");
        }
    }
}输出结果:
C-Hello混淆了,就必须重写,示例:
package com.example.study.java8.method;
/**
 * 1、类的优先级最高
 * 2、上一级接口,不管上一级接口有多少个类
 * 3、如果混淆了,就必须重写
 */
public class DefaultAction4 {
    public static void main(String[] args) {
        A c = new C();
        c.hello();
    }
    public interface A{
        default void hello(){
            System.out.println("A-Hello");
        }
    }
    public interface B {
        default void hello(){
            System.out.println("B-Hello");
        }
    }
    public static class C implements B , A{
//        @Override
//        public void hello() {
//            System.out.println("C-Hello");
//        }
    }
}类C如果不重写会报错。必须重写C中hello方法。
 
        
       
     
       
           
           
          
评论 (0)