zoukankan      html  css  js  c++  java
  • 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

      

    ----

  • 相关阅读:
    loj6033.「雅礼集训 2017 Day2」棋盘游戏
    loj6032. 「雅礼集训 2017 Day2」水箱
    BZOJ 5217 [Lydsy2017省队十连测] 航海舰队
    P4173 残缺的字符串
    P3723 [AH2017/HNOI2017]礼物
    P3321 [SDOI2015]序列统计
    P4841 [集训队作业2013]城市规划
    MySQL基础
    MySQL查询
    HTTP响应码
  • 原文地址:https://www.cnblogs.com/tenWood/p/11602964.html
Copyright © 2011-2022 走看看