JDK8接口的默认与静态方法

网友投稿 258 2023-01-04


JDK8接口的默认与静态方法

引入

JDK1.8后,接口允许定义默认方法与静态方法,如:Iterable类中的foreach方法。

public interface Iterable {

/**

* Returns an iterator over elements of type {@code T}.

*

* @return an Iterator.

*/

Iterator 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

*

The default implementation behaves as if:

*

* for (T t : this)

* action.accept(t);

* }

*

* @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);

}

}

...

}

可以发现,接口越来越和抽象类相似了。

设计初衷

以往,如果想在接口中新增方法比如foreach,他的子类(比如集合类)必须全部实现这个方法,这有点不现实,增加了default方法之后,就解决了这一问题,以前接口是对行为的抽象,纯设计模型,现在接口也承担了代码重构的一些责任,有利有弊吧.

方法冲突

一个类可以实现多个接口,也就是说有可能会发生默认方法冲突,有以下几种情况:

1、如果接口继承自另一个接口,这两个接口中有相同的默认方法,则保留父接口的默认方法。

2、如果一个类实现两个接口,其中一个是默认方法,另一个不管是默认方法还是抽象方法,都需要这个类重写这个方法。

3、接口中的默认方法与继承的父类中的方法冲突了,则优先选择父类的方法,这就兼容了以前的版本,名词叫类优先原则。

接口静态方法

接口中的静态方法定位就是工具方法,直接通过接口名调用。如:Comparator接口

/**

* Accepts a function that extracts a sort key from a type {@code T}, and

* returns a {@code Comparator

* the specified {@link Comparator}.

*

*

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,

*

*

{@code

* the specified {@link Comparator}.

*

*

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,

*

*

* Comparator cmp = Comparator.comparing(

* Person::getLasthttp://Name,

* String.CASE_INSENSITIVE_ORDER);

* }

*

* @param the type of element to be compared

* @param 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 Comparator comparing(

Function super T, ? extends U> keyExtractor,

Comparator super U> keyComparator)

{

Objects.requireNonNull(keyExtractor);

Objects.requireNonNull(keyComparator);

return (Comparator & Serializable)

(c1, c2) -> keyComparator.compare(keyExtractor.apply(c1),

keyExtractor.apply(c2));

}

JDK8抽象类与接口的区别

相同点:

1、都是抽象类型;

2、都可以实现方法;

3、都可以不依赖与实现者或者继承者去实现方法。

不同点:

1、抽象类不能多继承,接口可以;

2、抽象类与接口的设计理念不同,抽象类是is-a,接口是like-a,抽象类是对类的抽象,接口是对行为的抽象。

3、成员方法访问的不同,抽象类允许非final属性,允许方法是public,private和protected的,但是接口只允许常量属性,方法都是public的。

选型

如果你关系属性和方法的访问权限,那就考虑抽象类,如果你重点关注多重继承,考虑接口。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:如何让Win10实现Java文件的开机自启动
下一篇:vue实现接口测试工具(vue自动化测试工具)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~