您的位置:首页 > 编程语言 > Java开发

Java8接口的默认方法

2016-01-23 09:54 651 查看

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();
}
}

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

默认方法的多重继承
如下所示代码,

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接口中的默认方法有调用了父接口中的默认方法。

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

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();

另一个java8的接口默认方法实例

java8新增了接口的默认方法, 也就是说在接口中也可以有实现了, 这个实现方法是默认的实现,你也可以在接口的实现类里对此默认方法进行重写。

如下实例:

public class AppInterfaceDefaultMethod {
public static interface DefaultMethodDemo {
//定义默认方法, 默认方法前面加default关键字, 后面跟方法声明和方法体
default void demo(String input) {
System.out.println(input);
}
void doSomething();
}
public static class DemoClass implements DefaultMethodDemo {
@Override
public void doSomething() {
System.out.println("do something");
}
}
public static class DemoClassOverrideDemo implements DefaultMethodDemo {
//重写了默认方法
@Override
public void demo(String input) {
System.out.println("demo " + input + " by override method");
}
@Override
public void doSomething() {
System.out.println("do something");
}
}
public static void main(String[] args) {
DefaultMethodDemo demo = new DemoClass();
demo.demo("abc");
DefaultMethodDemo demoOverride = new DemoClassOverrideDemo();
demoOverride.demo("abc");
}
}

以上就是关于Java8接口的默认方法详细介绍,希望对大家的学习有所帮助。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java8 接口 默认方法