现在的位置: 首页 > 编程语言 > 正文

JDK8接口的默认与静态方法-接口与抽象类的区别详解

2020年02月14日 编程语言 ⁄ 共 2683字 ⁄ 字号 评论关闭

引入

JDK1.8后,接口允许定义默认方法与静态方法,如: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);}}...}

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

设计初衷

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

方法冲突

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

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

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

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

接口静态方法接口中的静态方法定位就是工具方法,直接通过接口名调用。如: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抽象类与接口的区别

相同点:

1、都是抽象类型;

2、都可以实现方法;

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

不同点:

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

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

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

选型

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

本文标题: JDK8接口的默认与静态方法-接口与抽象类的区别详解

以上就上有关JDK8接口的默认与静态方法-接口与抽象类的区别详解的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.