亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java8接口的默認(rèn)方法

 更新時(shí)間:2016年01月23日 09:54:59   投稿:lijiao  
這篇文章主要為大家介紹了Java8接口的默認(rèn)方法,還為大家默認(rèn)方法的多重繼承,感興趣的朋友可以參考一下

Java8接口的默認(rèn)方法

什么是默認(rèn)方法,為什么要有默認(rèn)方法?

簡(jiǎn)單說(shuō),就是接口可以有實(shí)現(xiàn)方法,而且不需要實(shí)現(xiàn)類(lèi)去實(shí)現(xiàn)其方法。只需在方法名前面加個(gè)default關(guān)鍵字即可。

為什么要有這個(gè)特性?首先,之前的接口是個(gè)雙刃劍,好處是面向抽象而不是面向具體編程,缺陷是,當(dāng)需要修改接口時(shí)候,需要修改全部實(shí)現(xiàn)該接口的類(lèi),目前的 java 8之前的集合框架沒(méi)有foreach方法,通常能想到的解決辦法是在JDK里給相關(guān)的接口添加新的方法及實(shí)現(xiàn)。然而,對(duì)于已經(jīng)發(fā)布的版本,是沒(méi)法在給接口添加新方法的同時(shí)不影響已有的實(shí)現(xiàn)。所以引進(jìn)的默認(rèn)方法。他們的目的是為了使接口沒(méi)有引入與現(xiàn)有的實(shí)現(xiàn)不兼容發(fā)展。

如以下所示,

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

聲明了一個(gè)接口,里面只有一個(gè)默認(rèn)方法。然后寫(xiě)一個(gè)具體的類(lèi)實(shí)現(xiàn)這個(gè)接口,

public class Dog implements Animal {
  public void sayHi() {
    System.out.println("dog");
  }
 
  public static void main(String args[]) {
    Dog dog = new Dog();
    dog.eat();
  }
}

再具體的類(lèi)里面不是必須重寫(xiě)默認(rèn)方法,但必須要實(shí)現(xiàn)抽象方法。

默認(rèn)方法的多重繼承
如下所示代碼,

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();//調(diào)用B的方法
    obj.doSomething();
  }
}

打印結(jié)果:

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()調(diào)用的是B接口中的默認(rèn)方法。同時(shí)在B接口中的默認(rèn)方法有調(diào)用了父接口中的默認(rèn)方法。

我們?cè)賮?lái)看一個(gè)例子,思考一下在多重繼承中,如果出現(xiàn)了同名的默認(rèn)方法,如下所示,

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

我們需要制定調(diào)用哪個(gè)接口的默認(rèn)方法如下,

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

另一個(gè)java8的接口默認(rèn)方法實(shí)例

java8新增了接口的默認(rèn)方法, 也就是說(shuō)在接口中也可以有實(shí)現(xiàn)了, 這個(gè)實(shí)現(xiàn)方法是默認(rèn)的實(shí)現(xiàn),你也可以在接口的實(shí)現(xiàn)類(lèi)里對(duì)此默認(rèn)方法進(jìn)行重寫(xiě)。

如下實(shí)例:

public class AppInterfaceDefaultMethod {
 
  public static interface DefaultMethodDemo {
    //定義默認(rèn)方法, 默認(rèn)方法前面加default關(guān)鍵字, 后面跟方法聲明和方法體
    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 {
    //重寫(xiě)了默認(rèn)方法
    @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");
  }
}

以上就是關(guān)于Java8接口的默認(rèn)方法詳細(xì)介紹,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • java實(shí)現(xiàn)批量生成二維碼

    java實(shí)現(xiàn)批量生成二維碼

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)批量生成二維碼的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • mybatis 事務(wù)回滾配置操作

    mybatis 事務(wù)回滾配置操作

    這篇文章主要介紹了mybatis 事務(wù)回滾配置操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • 快速上手Java單元測(cè)試框架JUnit5

    快速上手Java單元測(cè)試框架JUnit5

    今天給大家?guī)?lái)的是關(guān)于Java單元測(cè)試的相關(guān)知識(shí),文章圍繞著Java單元測(cè)試框架JUnit5展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • Springcloud Stream消息驅(qū)動(dòng)工具使用介紹

    Springcloud Stream消息驅(qū)動(dòng)工具使用介紹

    SpringCloud Stream由一個(gè)中間件中立的核組成,應(yīng)用通過(guò)SpringCloud Stream插入的input(相當(dāng)于消費(fèi)者consumer,它是從隊(duì)列中接收消息的)和output(相當(dāng)于生產(chǎn)者producer,它是發(fā)送消息到隊(duì)列中的)通道與外界交流
    2022-09-09
  • 解決spring-cloud-config 多服務(wù)共享公共配置的問(wèn)題

    解決spring-cloud-config 多服務(wù)共享公共配置的問(wèn)題

    這篇文章主要介紹了解決spring-cloud-config 多服務(wù)共享公共配置的問(wèn)題,本文通過(guò)多種方法給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • spring batch使用reader讀數(shù)據(jù)的內(nèi)存容量問(wèn)題詳解

    spring batch使用reader讀數(shù)據(jù)的內(nèi)存容量問(wèn)題詳解

    這篇文章主要介紹了spring batch使用reader讀數(shù)據(jù)的內(nèi)存容量問(wèn)題詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Spring Security入門(mén)demo案例

    Spring Security入門(mén)demo案例

    Spring Security是一個(gè)高度自定義的安全框架,本文主要介紹了Spring Security入門(mén),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Springboot FeignClient調(diào)用Method has too many Body parameters解決

    Springboot FeignClient調(diào)用Method has too m

    本文主要介紹了Springboot FeignClient微服務(wù)間調(diào)用Method has too many Body parameters 解決,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 使用Easyexcel實(shí)現(xiàn)不同場(chǎng)景的數(shù)據(jù)導(dǎo)出功能

    使用Easyexcel實(shí)現(xiàn)不同場(chǎng)景的數(shù)據(jù)導(dǎo)出功能

    這篇文章主要為大家詳細(xì)介紹了如何在不同場(chǎng)景下使用Easyexcel實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • sa-token?路由攔截式鑒權(quán)使用示例詳解

    sa-token?路由攔截式鑒權(quán)使用示例詳解

    這篇文章主要為大家介紹了sa-token?路由攔截式鑒權(quán)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評(píng)論