溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2020-10-21 15:57:27 來(lái)源:腳本之家 閱讀:136 作者:clawhub 欄目:編程語(yǔ)言

引入

JDK1.8后,接口允許定義默認(rèn)方法與靜態(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),接口越來(lái)越和抽象類相似了。

設(shè)計(jì)初衷

以往,如果想在接口中新增方法比如foreach,他的子類(比如集合類)必須全部實(shí)現(xiàn)這個(gè)方法,這有點(diǎn)不現(xiàn)實(shí),增加了default方法之后,就解決了這一問(wèn)題,以前接口是對(duì)行為的抽象,純?cè)O(shè)計(jì)模型,現(xiàn)在接口也承擔(dān)了代碼重構(gòu)的一些責(zé)任,有利有弊吧.

方法沖突

一個(gè)類可以實(shí)現(xiàn)多個(gè)接口,也就是說(shuō)有可能會(huì)發(fā)生默認(rèn)方法沖突,有以下幾種情況:

1、如果接口繼承自另一個(gè)接口,這兩個(gè)接口中有相同的默認(rèn)方法,則保留父接口的默認(rèn)方法。

2、如果一個(gè)類實(shí)現(xiàn)兩個(gè)接口,其中一個(gè)是默認(rèn)方法,另一個(gè)不管是默認(rèn)方法還是抽象方法,都需要這個(gè)類重寫(xiě)這個(gè)方法。

3、接口中的默認(rèn)方法與繼承的父類中的方法沖突了,則優(yōu)先選擇父類的方法,這就兼容了以前的版本,名詞叫類優(yōu)先原則。

接口靜態(tài)方法
接口中的靜態(tài)方法定位就是工具方法,直接通過(guò)接口名調(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ū)別

相同點(diǎn):

1、都是抽象類型;

2、都可以實(shí)現(xiàn)方法;

3、都可以不依賴與實(shí)現(xiàn)者或者繼承者去實(shí)現(xiàn)方法。

不同點(diǎn):

1、抽象類不能多繼承,接口可以;

2、抽象類與接口的設(shè)計(jì)理念不同,抽象類是is-a,接口是like-a,抽象類是對(duì)類的抽象,接口是對(duì)行為的抽象。

3、成員方法訪問(wèn)的不同,抽象類允許非final屬性,允許方法是public,private和protected的,但是接口只允許常量屬性,方法都是public的。

選型

如果你關(guān)系屬性和方法的訪問(wèn)權(quán)限,那就考慮抽象類,如果你重點(diǎn)關(guān)注多重繼承,考慮接口。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI