Java8接口的默认方法

时间:2015-09-18 13:56:41   收藏:0   阅读:134

Java8接口的默认方法

什么是默认方法,为什么要有默认方法

简单说,就是接口可以有实现方法,而且不需要实现类去实现其方法。只需在方法名前面加个default关键字即可。 

为什么要有这个特性?首先,之前的接口是个双刃剑,好处是面向抽象而不是面向具体编程,缺陷是,当需要修改接口时候,需要修改全部实现该接口的类,目前的 java 8之前的集合框架没有foreach方法,通常能想到的解决办法是在JDK里给相关的接口添加新的方法及实现。然而,对于已经发布的版本,是没法在给接口添加新方法的同时不影响已有的实现。所以引进的默认方法。他们的目的是为了使接口没有引入与现有的实现不兼容发展。

如以下所示,

public interface Animal {
    default void eat() {
        System.out.println("animal eat default method");
    }
}

声明了一个接口,里面只有一个默认方法。然后写一个具体的类实现这个接口,

public class Dog implements Animal {
    public void sayHi() {
        System.out.println("dog");
    }

    public static void main(String args[]) {
        Dog dog = new Dog();
        dog.eat();
    }
}

再具体的类里面不是必须重写默认方法,但必须要实现抽象方法。


默认方法的多重继承

如下所示代码,

/**
 * Created by xinxingegeya on 15/9/18.
 */
public interface A {

    void doSomething();

    default void hello() {
        System.out.println("hello world from interface A");
    }

    default void foo() {
        System.out.println("foo from interface A");
    }
}

interface B extends A {
    default void hello() {
        System.out.println("hello world from interface B");
        A.super.hello();
        this.foo();
        A.super.foo();
    }
}

class C implements B, A {

    @Override
    public void doSomething() {
        System.out.println("c object need do something");
    }

    public static void main(String args[]) {
        A obj = new C();
        obj.hello();//调用B的方法
        obj.doSomething();
    }
}

打印结果:

hello world from interface B

hello world from interface A

foo from interface A

foo from interface A

c object need do something

obj.hello()调用的是B接口中的默认方法。同时在B接口中的默认方法有调用了父接口中的默认方法。

我们再来看一个例子,思考一下在多重继承中,如果出现了同名的默认方法,如下所示,

/**
 * Created by xinxingegeya on 15/9/18.
 */
public interface D {
    default void hello() {
        System.out.println("hello world from D");
    }
}

interface E {
    default void hello() {
        System.out.println("hello world from E");
    }
}

class F implements D, E {

    @Override
    public void hello() {
        System.out.println("hello world F class");
        D.super.hello();
        E.super.hello();
    }

    public static void main(String args[]) {
        F f = new F();
        f.hello();
    }

}

我们需要制定调用哪个接口的默认方法如下,

 D.super.hello();
 E.super.hello();

======END======

原文:http://my.oschina.net/xinxingegeya/blog/508046

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!