JAVA8--默认方法

JAVA8--默认方法

      今天这个内容并不多比较轻松一点,默认方法在JAVA8的出现更加的是为了弥补这次JAVA8接口升级所带来的一系列问题。对于一些旧的接口进行升级一般我们都无可避免需要对实现这个接口的所有类进行新接口方法的实现。事实上这个东西是有必要的吗?答案未必··· 我们都知道JAVA当中是没有多继承这一说,不像是C++等其他语言拥有多继承的特性。当初我看过很多书本都说JAVA使用单一继承,完美避免了继承混乱的问题,今天JAVA8出了接口的默认方法特性,真的有点打脸打到啪啪响的意思。

       JAVA8 所提供的默认方法,就是在接口上你可以定义某些方法的实现。有点像是抽象类的味道,但是接口是可以让类实现多个,而抽象类的实现类只能继承一个抽象类。为什么要这么做呢?更多原因是因为JAVA的旧接口发生了比较多的变化,但是这种变化所导致的问题也是相当大的,所有实现这个接口的类必须都要去实现这些新的接口方法,这可能导致重大的项目兼容问题。所以才提供了默认方法这一说。首先我们看看有默认方法的接口长什么样子。

public interface DemoInterface {
    
    void hello();
    
    default void newFunction(){
        System.out.println("新接口方法");
    }
    
}

我们可以看到有一个default的开头的方法,这个就是我们的默认方法。那我们看看原来的实现类需要实现什么

public class DemoImplement implements DemoInterface {

    @Override
    public void hello() {
        System.out.println("Demo Implement hello!");
    }
}

我们只需要实现hello方法即可,完全不需要实现newFunction的默认方法。我们来看看调用如何:

public class DefaultMain {

    public static final void main(String[] args){
        DemoImplement demoImplement  = new DemoImplement();
        demoImplement.hello();
        demoImplement.newFunction();
    }

}

输出如下

Demo Implement hello!
新接口方法

嗯~ 没错默认方法就只有这些东西而已,但是还有些东西要说清楚,就是二义性。如果多个接口有相同的接口签名怎么办呢?会抛错····

public interface OtherInterface {

    default void newFunction(){
        System.out.println("新接口方法");
    }

}
public interface DemoInterface {

    void hello();

    default void newFunction(){
        System.out.println("新接口方法");
    }

}
public class DemoImplement implements DemoInterface,OtherInterface {

    @Override
    public void hello() {
        System.out.println("Demo Implement hello!");
    }
}

这样的代码是不合法的,我们必须在实现类当中自己实现其默认方法,或者在实现当中显现调用其中一个接口的默认方法。

public class DemoImplement implements DemoInterface,OtherInterface {

    @Override
    public void hello() {
        System.out.println("Demo Implement hello!");
    }

    @Override
    public void newFunction() {
        OtherInterface.super.newFunction();
    }
}

这里可以看出如果实现类实现了newFunction这个默认方法,调用时会直接使用DemoImplement的newFunction 而不是接口中的默认实现,这个跟之前的JAVA继承类似。

另外一种情况就是如果DemoInterface 继承与 OtherInterface的情况,这种情况有两个接口都定义了这个默认方法。调用的会是子接口的默认方法,也就是DemoInterface的默认方法,具体例子如下:

public interface OtherInterface {
    default void newFunction(){
        System.out.println("OtherInterface 新接口方法");
    }
}

public interface DemoInterface extends OtherInterface {
    void hello();
    default void newFunction(){
        System.out.println("DemoInterface 新接口方法");
    }
}

public class DemoImplement implements DemoInterface {
    @Override
    public void hello() {
        System.out.println("Demo Implement hello!");
    }
}

public class DefaultMain {

    public static final void main(String[] args){
        DemoImplement demoImplement  = new DemoImplement();
        demoImplement.newFunction();
    }

}

输出结果如下:

DemoInterface 新接口方法

Process finished with exit code 0

JAVA8 还有一个新的日期处理类,再后续会继续讲解。这段时间可能会换一下其他内容写写,老是写语法的内容会比较沉闷一些。默认方法的内容就差不多啦。

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值