JDK8接口的默認與靜態(tài)方法-接口與抽象類的區(qū)別詳解
引入
JDK1.8后,接口允許定義默認方法與靜態(tài)方法,如:Iterable類中的foreach方法。
public interface Iterable<T> { /** * Returns an iterator over elements of type {@code T}. * * @return an Iterator. */ Iterator<T> iterator(); /** * Performs the given action for each element of the {@code Iterable} * until all elements have been processed or the action throws an * exception. Unless otherwise specified by the implementing class, * actions are performed in the order of iteration (if an iteration order * is specified). Exceptions thrown by the action are relayed to the * caller. * * @implSpec * <p>The default implementation behaves as if: * <pre>{@code * for (T t : this) * action.accept(t); * }</pre> * * @param action The action to be performed for each element * @throws NullPointerException if the specified action is null * @since 1.8 */ default void forEach(Consumer<? super T> action) { Objects.requireNonNull(action); for (T t : this) { action.accept(t); } } ... }
可以發(fā)現(xiàn),接口越來越和抽象類相似了。
設計初衷
以往,如果想在接口中新增方法比如foreach,他的子類(比如集合類)必須全部實現(xiàn)這個方法,這有點不現(xiàn)實,增加了default方法之后,就解決了這一問題,以前接口是對行為的抽象,純設計模型,現(xiàn)在接口也承擔了代碼重構(gòu)的一些責任,有利有弊吧.
方法沖突
一個類可以實現(xiàn)多個接口,也就是說有可能會發(fā)生默認方法沖突,有以下幾種情況:
1、如果接口繼承自另一個接口,這兩個接口中有相同的默認方法,則保留父接口的默認方法。
2、如果一個類實現(xiàn)兩個接口,其中一個是默認方法,另一個不管是默認方法還是抽象方法,都需要這個類重寫這個方法。
3、接口中的默認方法與繼承的父類中的方法沖突了,則優(yōu)先選擇父類的方法,這就兼容了以前的版本,名詞叫類優(yōu)先原則。
接口靜態(tài)方法
接口中的靜態(tài)方法定位就是工具方法,直接通過接口名調(diào)用。如:Comparator接口
/** * Accepts a function that extracts a sort key from a type {@code T}, and * returns a {@code Comparator<T>} that compares by that sort key using * the specified {@link Comparator}. * * <p>The returned comparator is serializable if the specified function * and comparator are both serializable. * * @apiNote * For example, to obtain a {@code Comparator} that compares {@code * Person} objects by their last name ignoring case differences, * * <pre>{@code * Comparator<Person> cmp = Comparator.comparing( * Person::getLastName, * String.CASE_INSENSITIVE_ORDER); * }</pre> * * @param <T> the type of element to be compared * @param <U> the type of the sort key * @param keyExtractor the function used to extract the sort key * @param keyComparator the {@code Comparator} used to compare the sort key * @return a comparator that compares by an extracted key using the * specified {@code Comparator} * @throws NullPointerException if either argument is null * @since 1.8 */ public static <T, U> Comparator<T> comparing( Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) { Objects.requireNonNull(keyExtractor); Objects.requireNonNull(keyComparator); return (Comparator<T> & Serializable) (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1), keyExtractor.apply(c2)); }
JDK8抽象類與接口的區(qū)別
相同點:
1、都是抽象類型;
2、都可以實現(xiàn)方法;
3、都可以不依賴與實現(xiàn)者或者繼承者去實現(xiàn)方法。
不同點:
1、抽象類不能多繼承,接口可以;
2、抽象類與接口的設計理念不同,抽象類是is-a,接口是like-a,抽象類是對類的抽象,接口是對行為的抽象。
3、成員方法訪問的不同,抽象類允許非final屬性,允許方法是public,private和protected的,但是接口只允許常量屬性,方法都是public的。
選型
如果你關(guān)系屬性和方法的訪問權(quán)限,那就考慮抽象類,如果你重點關(guān)注多重繼承,考慮接口。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Springboot如何優(yōu)雅的進行數(shù)據(jù)校驗
基于?Spring?Boot?,如何“優(yōu)雅”的進行數(shù)據(jù)校驗呢,本文將待大家詳細介紹Springboot如何優(yōu)雅的進行數(shù)據(jù)校驗,文中有詳細的代碼示例和流程步驟,需要的朋友可以參考下2023-06-06Java工程編碼格式由GBK轉(zhuǎn)化成utf-8的具體實現(xiàn)
在寫項目的過程中我發(fā)現(xiàn)有的地方編碼格式被設置成了 gbk 如果用eclipse等工具直接改回utf-8編碼格式則會出現(xiàn)亂碼,所以本文給大家介紹了Java工程編碼格式由GBK轉(zhuǎn)化成utf-8的具體實現(xiàn),感興趣的朋友可以參考下2024-05-05Java中為什么重寫equals()也需要重寫hashCode方法
這篇文章主要介紹了Java中為什么重寫equals()也需要重寫hashCode(),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04Java中volatile關(guān)鍵字的作用與用法詳解
volatile關(guān)鍵字雖然從字面上理解起來比較簡單,但是要用好不是一件容易的事情。這篇文章主要介紹了Java中volatile關(guān)鍵字的作用與用法詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09Java Web監(jiān)聽器Listener接口原理及用法實例
這篇文章主要介紹了Java Web監(jiān)聽器Listener接口原理及用法實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-06-06