最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8接口之默認(rèn)方法與靜態(tài)方法詳解

 更新時(shí)間:2022年03月30日 08:23:28   作者:老K的Java博客  
java8中為接口新增了一項(xiàng)功能,定義一個(gè)或者更多個(gè)靜態(tài)方法,類似于類中的靜態(tài)方法,接口定義的靜態(tài)方法可以獨(dú)立于任何對象調(diào)用,下面這篇文章主要給大家介紹了關(guān)于Java8接口之默認(rèn)方法與靜態(tài)方法的相關(guān)資料,需要的朋友可以參考下

前言

在Java8之前,java中的接口只能有抽象方法。默認(rèn)情況下,接口的所有方法都是公共和抽象的。Java8允許接口具有默認(rèn)和靜態(tài)方法。我們在接口中使用默認(rèn)方法的原因是,允許開發(fā)人員向接口添加新方法,而不會影響實(shí)現(xiàn)這些接口的類。

為什么選擇默認(rèn)方法?

例如,如果A、B、C和D等幾個(gè)類實(shí)現(xiàn)了一個(gè)接口XYZInterface,那么如果我們向XYZInterface添加一個(gè)新方法,我們必須更改實(shí)現(xiàn)該接口的所有類(A、B、C和D)中的代碼。在本例中,我們只有四個(gè)類實(shí)現(xiàn)了我們想要更改的接口,但是想象一下,如果有數(shù)百個(gè)類實(shí)現(xiàn)了一個(gè)接口,那么幾乎不可能更改所有這些類中的代碼。這就是為什么在Java8中,我們有了一個(gè)新概念“默認(rèn)方法”。這些方法可以添加到任何現(xiàn)有接口中,我們不需要強(qiáng)制在實(shí)現(xiàn)類中實(shí)現(xiàn)這些方法,因此我們可以在不破壞代碼的情況下將這些默認(rèn)方法添加到現(xiàn)有接口中。

我們可以說,java 8中引入了默認(rèn)方法的概念,以便在現(xiàn)有接口中添加新方法,從而使它們向后兼容。向后兼容性是在不破壞舊代碼的情況下添加新功能。

接口中的靜態(tài)方法與默認(rèn)方法類似,只是我們不能在實(shí)現(xiàn)這些接口的類中重寫這些方法。

Java 8示例:接口中的默認(rèn)方法

MyInterface中的方法newMethod()是默認(rèn)方法,這意味著我們不需要在實(shí)現(xiàn)類示例中實(shí)現(xiàn)該方法。通過這種方式,我們可以將默認(rèn)方法添加到現(xiàn)有接口中,而不必?fù)?dān)心實(shí)現(xiàn)這些接口的類。

interface MyInterface{  
    /* This is a default method so we need not
     * to implement this method in the implementation 
     * classes  
     */
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    /* Already existing public and abstract method
     * We must need to implement this method in 
     * implementation classes.
     */
    void existingMethod(String str);  
}  
public class Example implements MyInterface{ 
    // implementing abstract method
    public void existingMethod(String str){           
        System.out.println("String is: "+str);  
    }  
    public static void main(String[] args) {  
        Example obj = new Example();
        
        //calling the default method of interface
        obj.newMethod();     
        //calling the abstract method of interface
        obj.existingMethod("Java 8 is easy to learn"); 
  
    }  
}

輸出:

Newly added default method
String is: Java 8 is easy to learn

Java 8示例:接口中的靜態(tài)方法

如上所述,接口中的靜態(tài)方法與默認(rèn)方法類似,因此我們不需要在實(shí)現(xiàn)類中實(shí)現(xiàn)它們。我們可以安全地將它們添加到現(xiàn)有接口中,而無需更改實(shí)現(xiàn)類中的代碼。由于這些方法是靜態(tài)的,我們不能在實(shí)現(xiàn)類中重寫它們。

interface MyInterface{  
    /* This is a default method so we need not
     * to implement this method in the implementation 
     * classes  
     */
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    
    /* This is a static method. Static method in interface is
     * similar to default method except that we cannot override 
     * them in the implementation classes.
     * Similar to default methods, we need to implement these methods
     * in implementation classes so we can safely add them to the 
     * existing interfaces.
     */
    static void anotherNewMethod(){
        System.out.println("Newly added static method");
    }
    /* Already existing public and abstract method
     * We must need to implement this method in 
     * implementation classes.
     */
    void existingMethod(String str);  
}  
public class Example implements MyInterface{ 
    // implementing abstract method
    public void existingMethod(String str){           
        System.out.println("String is: "+str);  
    }  
    public static void main(String[] args) {  
        Example obj = new Example();
        
        //calling the default method of interface
        obj.newMethod();     
        //calling the static method of interface
        MyInterface.anotherNewMethod();
        //calling the abstract method of interface
        obj.existingMethod("Java 8 is easy to learn"); 
    }  
}

輸出:

Newly added default method
Newly added static method
String is: Java 8 is easy to learn

Java 8 - 抽象類與接口

隨著接口中默認(rèn)方法的引入,抽象類似乎與Java8中的接口相同。然而,這并不是完全正確的,盡管我們現(xiàn)在可以像抽象類一樣在接口中有具體的方法(帶主體的方法),但這并不意味著它們是相同的。它們之間仍然沒有什么區(qū)別,其中之一是抽象類可以有構(gòu)造函數(shù),而在接口中我們不能有構(gòu)造函數(shù)。

接口的目的是提供完全抽象,而抽象類的目的是提供部分抽象。這仍然適用。界面就像是類的藍(lán)圖,通過引入默認(rèn)方法,您可以簡單地說,我們可以在界面中添加附加功能,而不會影響最終用戶類。

默認(rèn)方法和多重繼承

當(dāng)我們有兩個(gè)具有相同簽名的默認(rèn)方法的接口時(shí),可能會出現(xiàn)多重繼承問題。讓我們舉個(gè)例子。

interface MyInterface{  
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    void existingMethod(String str);  
}  
interface MyInterface2{  
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    void disp(String str);  
} 
public class Example implements MyInterface, MyInterface2{ 
    // implementing abstract methods
    public void existingMethod(String str){           
        System.out.println("String is: "+str);  
    }  
    public void disp(String str){
        System.out.println("String is: "+str); 
    }
    
    public static void main(String[] args) {  
        Example obj = new Example();
        //calling the default method of interface
        obj.newMethod();     
    }  
}

輸出:

Error: Duplicate default methods named newMethod with the parameters () and () are inherited from the types MyInterface2 and MyInterface

這是因?yàn)槲覀冊诮涌谥卸加邢嗤姆椒ǎ幾g器不確定要調(diào)用哪個(gè)方法。

如何解決這個(gè)問題?

為了解決這個(gè)問題,我們可以在實(shí)現(xiàn)類中實(shí)現(xiàn)這個(gè)方法,如下所示:

interface MyInterface{  
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    void existingMethod(String str);  
}  
interface MyInterface2{  
     
    default void newMethod(){  
        System.out.println("Newly added default method");  
    }  
    void disp(String str);  
} 
public class Example implements MyInterface, MyInterface2{ 
    // implementing abstract methods
    public void existingMethod(String str){           
        System.out.println("String is: "+str);  
    }  
    public void disp(String str){
        System.out.println("String is: "+str); 
    }
    //Implementation of duplicate default method
    public void newMethod(){  
        System.out.println("Implementation of default method");  
    }  
    public static void main(String[] args) {  
        Example obj = new Example();
        //calling the default method of interface
        obj.newMethod();     
    }  
}

輸出:

Implementation of default method

首先我們要總體說一下,為什么要有這兩個(gè)方法存在:

(1)原先的jdk7之類的,它們接口中的方法都是抽象方法,沒有具體的實(shí)現(xiàn),就相當(dāng)于定義好了這個(gè)接口有哪些功能,卻沒有具體定義功能是怎么實(shí)現(xiàn)的,通常由接口的實(shí)現(xiàn)類來做具體功能實(shí)現(xiàn)。那么,如果面向接口編程,大家已經(jīng)根據(jù)自己需要通過繼承接口的方式來實(shí)現(xiàn)了自己的功能,突然有一天,產(chǎn)品提需求了,你需要給所有接口的實(shí)現(xiàn)類都添加一個(gè)新的功能即一個(gè)新的方法實(shí)現(xiàn),而且這個(gè)方法可能大家都是一樣的,那咋辦?

jdk8以前的做法肯定是現(xiàn)在接口中定義這個(gè)抽象方法,然后所有實(shí)現(xiàn)類必須實(shí)現(xiàn)這個(gè)方法(不然接口中多出一個(gè)抽象方法,其他類都沒有實(shí)現(xiàn),編譯是會報(bào)錯(cuò)的),如果實(shí)現(xiàn)類比較多,那改起來會很麻煩,這種情況下是不利于維護(hù)的。

那么我們在jdk8中就有了好的解決方式,就是在接口中加一個(gè)默認(rèn)方法,這個(gè)默認(rèn)方法有具體實(shí)現(xiàn),這樣就不用去修改實(shí)現(xiàn)類啦,很省事。

總結(jié)

到此這篇關(guān)于Java8接口之默認(rèn)方法與靜態(tài)方法的文章就介紹到這了,更多相關(guān)Java8默認(rèn)方法與靜態(tài)方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java反射機(jī)制給實(shí)體類相同字段自動(dòng)賦值實(shí)例

    java反射機(jī)制給實(shí)體類相同字段自動(dòng)賦值實(shí)例

    這篇文章主要介紹了java反射機(jī)制給實(shí)體類相同字段自動(dòng)賦值實(shí)例,具有
    2020-08-08
  • Java實(shí)用工具之使用oshi獲取主機(jī)信息的方法

    Java實(shí)用工具之使用oshi獲取主機(jī)信息的方法

    這篇文章主要介紹了Java實(shí)用工具之使用oshi獲取主機(jī)信息的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • Java實(shí)現(xiàn)字符串反轉(zhuǎn)

    Java實(shí)現(xiàn)字符串反轉(zhuǎn)

    這篇文章介紹了Java實(shí)現(xiàn)字符串反轉(zhuǎn)的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Java8 Stream Collectors收集器使用方法解析

    Java8 Stream Collectors收集器使用方法解析

    這篇文章主要介紹了Java8 Stream Collectors收集器使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例

    SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例

    這篇文章主要介紹了SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-04-04
  • Java由淺入深分析多態(tài)的概念

    Java由淺入深分析多態(tài)的概念

    多態(tài)就是指程序中定義的引用變量所指向的具體類型和通過該引用變量發(fā)出的方法調(diào)用在編程時(shí)并不確定,而是在程序運(yùn)行期間才確定,即一個(gè)引用變量到底會指向哪個(gè)類的實(shí)例對象,該引用變量發(fā)出的方法調(diào)用到底是哪個(gè)類中實(shí)現(xiàn)的方法,必須在由程序運(yùn)行期間才能決定
    2022-04-04
  • Java 手寫LRU緩存淘汰算法

    Java 手寫LRU緩存淘汰算法

    本文主要講了如何通過哈希鏈表這種數(shù)據(jù)結(jié)構(gòu)來實(shí)現(xiàn)LRU算法,提供了三種實(shí)現(xiàn)思路,第一種從雙向鏈表開始,借助于HashMap來實(shí)現(xiàn)滿足要求的LRUCache
    2021-05-05
  • Java實(shí)現(xiàn)圓形碰撞檢測

    Java實(shí)現(xiàn)圓形碰撞檢測

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圓形碰撞檢測,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Java實(shí)戰(zhàn)之晚會抽獎(jiǎng)系統(tǒng)的實(shí)現(xiàn)

    Java實(shí)戰(zhàn)之晚會抽獎(jiǎng)系統(tǒng)的實(shí)現(xiàn)

    這篇文章主要介紹了如何利用Java語言編寫一個(gè)晚會抽獎(jiǎng)系統(tǒng),文中采用到的技術(shù)有Jdbc、Servlert、JavaScript、JQuery、Ajax等,感興趣的可以學(xué)習(xí)一下
    2022-03-03
  • java跳出循環(huán)的方式匯總

    java跳出循環(huán)的方式匯總

    本文介紹了Java中三種常用的跳出循環(huán)的語句:break、continue和return,break用于完全結(jié)束循環(huán)并跳出循環(huán)體;continue用于跳過本次循環(huán)體中尚未執(zhí)行的語句,開始下一次循環(huán);return用于結(jié)束方法,每種方法給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2025-02-02

最新評論

内丘县| 化隆| 岗巴县| 德庆县| 巴南区| 深泽县| 南安市| 望奎县| 阿拉善盟| 呼伦贝尔市| 罗山县| 浦城县| 招远市| 金山区| 滦南县| 光泽县| 类乌齐县| 乾安县| 东兰县| 西贡区| 松滋市| 泽库县| 龙游县| 武定县| 登封市| 东光县| 会昌县| 石嘴山市| 米泉市| 嘉祥县| 灵山县| 玉门市| 滦平县| 博野县| 闸北区| 敦化市| 绥宁县| 广水市| 四川省| 金川县| 望城县|