当前位置: 移动技术网 > IT编程>开发语言>Java > Java8接口的默认方法

Java8接口的默认方法

2019年07月22日  | 移动技术网IT编程  | 我要评论
java8接口的默认方法 什么是默认方法,为什么要有默认方法? 简单说,就是接口可以有实现方法,而且不需要实现类去实现其方法。只需在方法名前面加个default关键字即

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接口的默认方法详细介绍,希望对大家的学习有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网