Java8接口的默認方法
Java8接口的默認方法
什么是默認方法,為什么要有默認方法?
簡單說,就是接口可以有實現(xiàn)方法,而且不需要實現(xiàn)類去實現(xiàn)其方法。只需在方法名前面加個default關(guān)鍵字即可。
為什么要有這個特性?首先,之前的接口是個雙刃劍,好處是面向抽象而不是面向具體編程,缺陷是,當需要修改接口時候,需要修改全部實現(xiàn)該接口的類,目前的 java 8之前的集合框架沒有foreach方法,通常能想到的解決辦法是在JDK里給相關(guān)的接口添加新的方法及實現(xiàn)。然而,對于已經(jīng)發(fā)布的版本,是沒法在給接口添加新方法的同時不影響已有的實現(xiàn)。所以引進的默認方法。他們的目的是為了使接口沒有引入與現(xiàn)有的實現(xiàn)不兼容發(fā)展。
如以下所示,
public interface Animal { default void eat() { System.out.println("animal eat default method"); } }
聲明了一個接口,里面只有一個默認方法。然后寫一個具體的類實現(xiàn)這個接口,
public class Dog implements Animal { public void sayHi() { System.out.println("dog"); } public static void main(String args[]) { Dog dog = new Dog(); dog.eat(); } }
再具體的類里面不是必須重寫默認方法,但必須要實現(xià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接口中的默認方法。同時在B接口中的默認方法有調(diào)用了父接口中的默認方法。
我們再來看一個例子,思考一下在多重繼承中,如果出現(xià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)用哪個接口的默認方法如下,
D.super.hello(); E.super.hello();
另一個java8的接口默認方法實例:
java8新增了接口的默認方法, 也就是說在接口中也可以有實現(xiàn)了, 這個實現(xiàn)方法是默認的實現(xiàn),你也可以在接口的實現(xiàn)類里對此默認方法進行重寫。
如下實例:
public class AppInterfaceDefaultMethod { public static interface DefaultMethodDemo { //定義默認方法, 默認方法前面加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 { //重寫了默認方法 @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接口的默認方法詳細介紹,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Springcloud Stream消息驅(qū)動工具使用介紹
SpringCloud Stream由一個中間件中立的核組成,應(yīng)用通過SpringCloud Stream插入的input(相當于消費者consumer,它是從隊列中接收消息的)和output(相當于生產(chǎn)者producer,它是發(fā)送消息到隊列中的)通道與外界交流2022-09-09解決spring-cloud-config 多服務(wù)共享公共配置的問題
這篇文章主要介紹了解決spring-cloud-config 多服務(wù)共享公共配置的問題,本文通過多種方法給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11spring batch使用reader讀數(shù)據(jù)的內(nèi)存容量問題詳解
這篇文章主要介紹了spring batch使用reader讀數(shù)據(jù)的內(nèi)存容量問題詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Springboot FeignClient調(diào)用Method has too m
本文主要介紹了Springboot FeignClient微服務(wù)間調(diào)用Method has too many Body parameters 解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12使用Easyexcel實現(xiàn)不同場景的數(shù)據(jù)導(dǎo)出功能
這篇文章主要為大家詳細介紹了如何在不同場景下使用Easyexcel實現(xiàn)數(shù)據(jù)導(dǎo)出功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03