• JDK8新特性:接口的静态方法和默认方法


    在jdk8之前,interface之中可以定义变量和方法,变量必须是public、static、final的,方法必须是public、abstract的。由于这些修饰符都是默认的,所以在JDK8之前,下面的写法都是等价的。

    [java] view plain copy
     
    1. public interface JDK8BeforeInterface {  
    2.     public static final int field1 = 0;  
    3.   
    4.     int field2 = 0;  
    5.   
    6.     public abstract void method1(int a) throws Exception;  
    7.   
    8.     void method2(int a) throws Exception;  
    9. }  

    JDK8及以后,允许我们在接口中定义static方法和default方法。

    [java] view plain copy
     
    1. public interface JDK8Interface {  
    2.   
    3.     // static修饰符定义静态方法  
    4.     static void staticMethod() {  
    5.         System.out.println("接口中的静态方法");  
    6.     }  
    7.   
    8.     // default修饰符定义默认方法  
    9.     default void defaultMethod() {  
    10.         System.out.println("接口中的默认方法");  
    11.     }  
    12. }  


    再定义一个接口的实现类:

    [java] view plain copy
     
    1. public class JDK8InterfaceImpl implements JDK8Interface {  
    2.     //实现接口后,因为默认方法不是抽象方法,所以可以不重写,但是如果开发需要,也可以重写  
    3. }  

    静态方法,只能通过接口名调用,不可以通过实现类的类名或者实现类的对象调用。default方法,只能通过接口实现类的对象来调用。

    [java] view plain copy
     
    1. public class Main {  
    2.     public static void main(String[] args) {  
    3.         // static方法必须通过接口类调用  
    4.         JDK8Interface.staticMethod();  
    5.   
    6.         //default方法必须通过实现类的对象调用  
    7.         new JDK8InterfaceImpl().defaultMethod();  
    8.     }  
    9. }  


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

    [java] view plain copy
     
    1. public class AnotherJDK8InterfaceImpl implements JDK8Interface {  
    2.       
    3.     // 签名跟接口default方法一致,但是不能再加default修饰符  
    4.     @Override  
    5.     public void defaultMethod() {  
    6.         System.out.println("接口实现类覆盖了接口中的default");  
    7.     }  
    8. }  



    由于java支持一个实现类可以实现多个接口,如果多个接口中存在同样的static和default方法会怎么样呢?如果有两个接口中的静态方法一模一样,并且一个实现类同时实现了这两个接口,此时并不会产生错误,因为jdk8只能通过接口类调用接口中的静态方法,所以对编译器来说是可以区分的。但是如果两个接口中定义了一模一样的默认方法,并且一个实现类同时实现了这两个接口,那么必须在实现类中重写默认方法,否则编译失败。

    [java] view plain copy
     
    1. public interface JDK8Interface1 {  
    2.   
    3.     // static修饰符定义静态方法  
    4.     static void staticMethod() {  
    5.         System.out.println("JDK8Interface1接口中的静态方法");  
    6.     }  
    7.   
    8.     // default修饰符定义默认方法  
    9.     default void defaultMethod() {  
    10.         System.out.println("JDK8Interface1接口中的默认方法");  
    11.     }  
    12.   
    13. }  
    [java] view plain copy
     
    1. public class JDK8InterfaceImpl implements JDK8Interface,JDK8Interface1 {  
    2.   
    3.     // 由于JDK8Interface和JDK8Interface1中default方法一样,所以这里必须覆盖  
    4.     @Override  
    5.     public void defaultMethod() {  
    6.         System.out.println("接口实现类覆盖了接口中的default");  
    7.     }  
    8. }  
    [java] view plain copy
     
    1. public class Main {  
    2.     public static void main(String[] args) {  
    3.         JDK8Interface.staticMethod();  
    4.         JDK8Interface1.staticMethod();  
    5.         new JDK8InterfaceImpl().defaultMethod();  
    6.     }  
    7. }  

  • 相关阅读:
    SQL Server, Timeout expired.all pooled connections were in use and max pool size was reached
    javascript 事件调用顺序
    Best Practices for Speeding Up Your Web Site
    C语言程序设计 使用VC6绿色版
    破解SQL Prompt 3.9的几步操作
    Master page Path (MasterPage 路径)
    几个小型数据库的比较
    CSS+DIV 完美实现垂直居中的方法
    由Response.Redirect引发的"Thread was being aborted. "异常的处理方法
    Adsutil.vbs 在脚本攻击中的妙用
  • 原文地址:https://www.cnblogs.com/yuechuan/p/8989989.html
Copyright © 2020-2023  润新知