当前位置: 移动技术网 > IT编程>开发语言>Java > JDK8接口的默认与静态方法-接口与抽象类的区别详解

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

2019年07月19日  | 移动技术网IT编程  | 我要评论
引入 jdk1.8后,接口允许定义默认方法与静态方法,如:iterable类中的foreach方法。 public interface iterable

引入

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

选型

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

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网