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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-04-03 我要评论

引入

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的。

选型

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

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们