Java8 新特性-接口中的默认方法与静态方法介绍

java8后允许在接口中定义默认方法和静态方法,具体实现见下文。

1、接口中默认方法创建和使用

默认方法使用 default 修饰符在方法前进行修饰。

package com.my.inter;

public interface MyInterface {
    //这是接口中的默认方法
    default String getString(){
        return "I'm interface inner function";
    }
}

我们创建一个实现该接口的类:

package com.my.inter;

public class ImpClass implements MyInterface{
}

使用Junit Test来做测试:

package com.my.inter;

public class Test {

    @org.junit.Test
    public void test(){
        ImpClass cls = new ImpClass();
        System.out.println(cls.getString());
    }
}

运行测试,获得如下结果:
在这里插入图片描述
可以看出实现了MyInterface的ImpClass的实例能够正常调用接口中的默认方法。

2、类优先原则

当实现接口类与接口中的默认方法一致时,将采用“类优先”原则,选择类中的方法,而不会使用接口中定义的默认方法。
接口文件与上节一致:

package com.my.inter;

public interface MyInterface {
    //这是接口中的默认方法
    default String getString(){
        return "I'm interface inner function";
    }
}

我们创建一个实现该接口的类,且类中的方法与接口方法名及结构一致:

package com.my.inter;

public class ImpClass implements MyInterface{
    //这是实现类中的默认方法
    public String getString() {
        return "I'm class inner function";
    }
}

使用Junit Test来做测试:

package com.my.inter;

public class Test {

    @org.junit.Test
    public void test(){
        ImpClass cls = new ImpClass();
        System.out.println(cls.getString());
    }
}

运行测试,获得如下结果:
在这里插入图片描述

3、子类同时继承父类和接口同名方法

接口文件内容不变:

package com.my.inter;

public interface MyInterface {
    //这是接口中的默认方法
    default String getString(){
        return "I'm interface inner function";
    }
}

父类存在于接口同名的方法:

package com.my.inter;

public class MyClass {
    public String getString() {
        return "I'm MyClass inner function";
    }
}

子类同时继承父类和接口:

package com.my.inter;

public class ImpClass  extends MyClass implements MyInterface{

}

使用Junit Test来做测试:

package com.my.inter;

public class Test {

    @org.junit.Test
    public void test(){
        ImpClass cls = new ImpClass();
        System.out.println(cls.getString());
    }
}

运行测试,获得如下结果:
在这里插入图片描述
当子类同时继承接口和父类的同名方法时,默认会调用父类中的同名方法,依然保持“类”优先的原则。

4、子类同时继承父类和接口同名方法,且子类也拥有同名方法

接口文件内容不变:

package com.my.inter;

public interface MyInterface {
    //这是接口中的默认方法
    default String getString(){
        return "I'm interface inner function";
    }
}

父类存在于接口同名的方法:

package com.my.inter;

public class MyClass {
    public String getString() {
        return "I'm MyClass inner function";
    }
}

子类同时继承父类和接口,并自身存在于父类和接口同名的方法:

package com.my.inter;

public class ImpClass  extends MyClass implements MyInterface{
    public String getString() {
        return "I'm ImpClass inner function";
    }
}

使用Junit Test来做测试:

package com.my.inter;

public class Test {

    @org.junit.Test
    public void test(){
        ImpClass cls = new ImpClass();
        System.out.println(cls.getString());
    }
}

运行测试,获得如下结果:
在这里插入图片描述
当子类同时继承接口和父类的同名方法,且子类也拥有同名方法时,默认会调用子类本身的方法,以接近原则进行调用。

5、实现多个拥有同名方法的接口

接口1:

package com.my.inter;

public interface MyInterface1 {
    //这是接口1中的默认方法
    default String getString(){
        return "I'm interface1 inner function";
    }
}

接口2:

package com.my.inter;

public interface MyInterface2 {
    //这是接口2中的默认方法
    default String getString(){
        return "I'm interface2 inner function";
    }
}

类同时实现接口1和接口2情况,方法必须重写,在重写时需要指明具体实现哪个接口的方法,写法如下:

package com.my.inter;

public class ImpClass  implements MyInterface1 ,MyInterface2{

    @Override
    public String getString() {
        return MyInterface2.super.getString();
    }
}

以上使用**MyInterface2.super.getString()**来说明使用具体接口的方法。

6、接口中静态方法创建和使用

在接口中定义静态方法和在类中定义静态方法的方式一致,使用关键字static进行方法修饰。
接口文件如下:

package com.my.inter;

public interface MyInterface {
    //这是接口中的静态方法
    public static void print(){
        System.out.println("I'm interface static function");
    }
}

在使用时直接使用“接口名.静态方法名”的方式直接调用:

package com.my.inter;

public class Test {

    @org.junit.Test
    public void test(){
        MyInterface.print();
    }
}

运行测试,获得如下结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值