资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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: *

{@code
* 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} that compares by that sort key using
* 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
* Comparator cmp = Comparator.comparing(
* Person::getLastName,
* 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的。

选型

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

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


网站题目:JDK8接口的默认与静态方法-接口与抽象类的区别详解
分享网址:http://cdkjz.cn/article/pdgdsc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220