JDK8接口的默認與靜態(tài)方法-接口與抽象類的區(qū)別詳解

引入

網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、微信平臺小程序開發(fā)、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了濟源免費建站歡迎大家使用!

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)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

文章標題:JDK8接口的默認與靜態(tài)方法-接口與抽象類的區(qū)別詳解
文章URL:http://muchs.cn/article32/pdgdsc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)商城網(wǎng)站、關(guān)鍵詞優(yōu)化網(wǎng)站制作、網(wǎng)站建設、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)