zoukankan      html  css  js  c++  java
  • Java8新特性(一)_interface中的static方法和default方法

    什么要单独写个Java8新特性,一个原因是我目前所在的公司用的是jdk8,并且框架中用了大量的Java8的新特性,如上篇文章写到的stream方法进行过滤map集合。stream方法就是接口Collection中的default方法。所以准备专门写写关于java8新特性的文章,虽然现在10已经发布了。但还是要认真的去了解下新版本的变化。

    static方法

    java8中为接口新增了一项功能:定义一个或者更多个静态方法。用法和普通的static方法一样。

    代码示例

    public interface InterfaceA {
    
        /**
         * 静态方法
         */
        static void showStatic() {
            System.out.println("InterfaceA++showStatic");
        }
    
    }

    测试

    public class Test {
        public static void main(String[] args) {
            InterfaceA.show();
        }
    }

    结果

    InterfaceA++showStatic

    注意,实现接口的类或者子接口不会继承接口中的静态方法

    default方法

    在接口中,增加default方法, 是为了既有的成千上万的Java类库的类增加新的功能, 且不必对这些类重新进行设计。 比如, 只需在Collection接口中
    增加default Stream stream(), 相应的Set和List接口以及它们的子类都包含此的方法, 不必为每个子类都重新copy这个方法。

    代码示例

    实现单一接口,仅实现接口
    public interface InterfaceA {
    
        /**
         * 静态方法
         */
        static void showStatic() {
            System.out.println("InterfaceA++showStatic");
        }
    
        /**
         * 默认方法
         */
        default void showDefault() {
            System.out.println("InterfaceA ++showDefault");
        }
    
    }
    
    
    
    /**先只实现这个接口
     * @author: curry
     * @Date: 2018/7/31
     */
    public class InterfaceAImpl implements InterfaceA{
    
    
    }

    测试

    public class Test {
        public static void main(String[] args) {
            InterfaceA.showStatic();
            new InterfaceAImpl().showDefault();
        }
    }

    结果

    InterfaceA++showStatic
    InterfaceA ++showDefault

    如果接口中的默认方法不能满足某个实现类需要,那么实现类可以覆盖默认方法。

    实现单一接口,重写接口中的default方法
    public class InterfaceAImpl implements InterfaceA{
    
        /**
         * 跟接口default方法一致,但不能再加default修饰符
         */
        @Override
        public void showDefault(){
            System.out.println("InterfaceAImpl++ defaultShow");
        }
    
    }

    测试

    public class Test {
        public static void main(String[] args) {
            InterfaceA.showStatic();
            new InterfaceAImpl().showDefault();
        }
    }

    结果

    InterfaceA++showStatic
    InterfaceAImpl++ defaultShow
    实现多个接口,且接口中拥有相同的default方法和static方法

    新创建个接口InterfaceB

    /**
     * @author: curry
     * @Date: 2018/7/31
     */
    public interface InterfaceB {
        /**
         * 静态方法
         */
        static void showStatic() {
            System.out.println("InterfaceB++showStatic");
        }
    
        /**
         * 默认方法
         */
        default void showDefault() {
            System.out.println("InterfaceB ++showDefault");
        }
    
    }

    修改实现类

    public class InterfaceAImpl implements InterfaceA,InterfaceB{
        @Override
        public void showDefault() {
            System.out.println("InterfaceAImpl ++ showDefault");
        }
        

    测试结果

    InterfaceA++showStatic
    InterfaceAImpl ++ showDefault

    总结

    看了接口中新增的这个新特性,感觉还是不错的,内容也比较简单。需要注意一点就是如果实现多个接口时,每个接口都有相同的default方法需要重写该方法。

    参考:Java8新特性(一)_interface中的static方法和default方法

  • 相关阅读:
    Oracle 按一行里某个字段里的值分割成多行进行展示
    Property or method "openPageOffice" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by
    SpringBoot 项目启动 Failed to convert value of type 'java.lang.String' to required type 'cn.com.goldenwater.dcproj.dao.TacPageOfficePblmListDao';
    Maven 设置阿里镜像
    JS 日期格式化,留作参考
    JS 过滤数组里对象的某个属性
    原生JS实现简单富文本编辑器2
    Chrome控制台使用详解
    android权限(permission)大全
    不借助第三方网站四步实现手机网站转安卓APP
  • 原文地址:https://www.cnblogs.com/aspirant/p/10736292.html
Copyright © 2011-2022 走看看