在 Java 8 中,引入了一個名為預設方法的新概念,以實現透過舊介面控制 lambda 表達式的向後相容性。此外,介面允許具有可實現的功能,而不會對將要實作介面的類別造成任何問題。在引入 Java 8 之前,介面僅允許抽象方法。此外,必須在不同的類別中提供功能。在以下部分中,將解釋預設方法的語法、工作原理和範例。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法
以下是預設方法的語法。
public interface animal { default void sound() { System.out.println("This is a sample default method. . .!"); }}
預設方法在 Java 中如何運作?
眾所周知,List、集合等介面沒有forEach方法。如果添加它,集合的框架實作將會中斷。由於Java 8中引入了預設方法,因此可以對方法forEach進行預設實作。除此之外,一個類別可以實作兩個具有相同預設功能的介面。讓我們看看程式碼的歧義是如何解決的。
public interface livingthings { default void sound() { . . . System.out.println("Living things too make noise . . .") ; } } public interface animals { default void sound() { . . . System.out.println("animals too make noise . . .") ; } }
對於這種歧義有兩種解決方案。
1. 要覆寫預設方法實現,請建立您自己的方法。
public class dogs implements livingthings,animals { default void sound() { . . . System.out.println("dogs bark . . .") ;} }
2.使用super,呼叫預設方法
public class dogs implements livingthings,animals { default void sound() { . . . livingthings.super.print("dogs bark . . .") ; } }
在Java中,通常一個類別可以實作n個介面。此外,一個介面可以透過另一個介面進行擴充。假設一個類別有兩個接口,並且實作了一個預設方法;特定的類別可能會在選擇考慮呼叫哪個方法時感到困惑。為了解決這些衝突,可以做到以下幾點。
- 匹配並呼叫類別中的重寫方法。
- 如果提供了相同方法,將選擇最合適的方法。假設有兩個接口,A 和 B 分別針對特定類別。如果A擴展了B,那麼A在這裡是最具體的,預設方法將從A中選擇。如果A和b是獨立的接口,就會發生嚴重的衝突情況,編譯器會抱怨它是無法選擇。在這種情況下,使用者必須透過提供額外的詳細資訊來幫助編譯器,從中選擇預設方法 A 或 B。例如
A.super.demo() ;
或
B.super.demo() ;
普通方法和預設方法的差異
現在,讓我們看看普通方法和預設方法之間的一些差異
- 與普通方法不同,預設方法帶有預設修飾符。
- 介面的參數對於預設方法沒有任何特定狀態。
- 普通方法可以使用以及更改方法和類別欄位的參數。
- 可以將新功能加入到現有介面中,而不是破壞這些特定介面的先前實作。
在擴充包含預設方法的介面時,可以執行下列操作。
- 預設方法不會被覆寫,並且會被繼承。
- 預設方法將被重寫,這與子類別中重寫的方法類似。
- 預設方法應再次宣告為抽象方法,這會強制子類別被重寫。
實作 Java 預設方法的範例
以下是提到的範例程式:
範例#1
實作預設方法的Java程式
代碼:
//Java program to implement default method public class DefExample { //main method public static void main(String args[]) { //create an object for the interface animal Animals obj = new Dog(); //call the print method obj.print( ); } } //create an interface animal interface Animals { //default method default void print( ) { System.out.println("I have four legs. . . ! !"); } static void sound() { System.out.println("I used to bark alot!!!") ; } } //create an interface <u>carnivores</u> interface Carnivores { //default method default void print( ) { System.out.println("I eat non veg. . .! !") ; } } //class that implements the other two interfaces class Dog implements Animals, Carnivores { public void print( ) { //call the print method of Animals using super Animals.super.print( ) ; //call the print method of Carnivores using super Carnivores.super.print( ); //call the sound method of Animals Animals.sound(); System.out.println("I am a Dog . . .!"); } }
輸出:
說明:在這個程式中,Animals 和 Carnivores 兩個介面有相同的預設方法 print(),並且使用 super 呼叫。
範例#2
實作預設方法的Java程式
代碼:
//Java program to implement default method interface Sampleinterface{ // Since this is declared as a default method, this has to be implemented in the implementation classes default void sammethod(){ System.out.println("a default method which is newly added to the program"); } // existing public as well as abstract methods has to be implemented in the implementation classes void oldmethod(String s); } public class DefExample implements Sampleinterface{ // abstract method implementation public void oldmethod(String s){ System.out.println("The string given is: "+ s); } public static void main(String[] args) { DefExample obj = new DefExample(); //call the default method obj.sammethod(); //call the abstract method obj.oldmethod("I am the old method in the program"); } }
輸出:
說明:在這個程式中,存在一個介面Sampleinterface,它有一個預設方法sammethod(),並且被呼叫。
結論
在 Java 8 中,提供了一個名為預設方法的新概念,用於在舊介面控制 lambda 表達式的情況下執行向後相容性。此外,介面參數對於預設方法沒有任何特定狀態。在本文中,詳細解釋了預設方法的語法、工作原理和範例。
以上是Java預設方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本文討論了使用Maven和Gradle進行Java項目管理,構建自動化和依賴性解決方案,以比較其方法和優化策略。

本文使用Maven和Gradle之類的工具討論了具有適當的版本控制和依賴關係管理的自定義Java庫(JAR文件)的創建和使用。

本文討論了使用咖啡因和Guava緩存在Java中實施多層緩存以提高應用程序性能。它涵蓋設置,集成和績效優勢,以及配置和驅逐政策管理最佳PRA

本文討論了使用JPA進行對象相關映射,並具有高級功能,例如緩存和懶惰加載。它涵蓋了設置,實體映射和優化性能的最佳實踐,同時突出潛在的陷阱。[159個字符]

Java的類上載涉及使用帶有引導,擴展程序和應用程序類負載器的分層系統加載,鏈接和初始化類。父代授權模型確保首先加載核心類別,從而影響自定義類LOA


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

記事本++7.3.1
好用且免費的程式碼編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版