對(duì)于Java系程序猿而言,大家對(duì)于接口一定不會(huì)陌生,畢竟經(jīng)常與之打交道的。我們對(duì)于Java接口的功能和使用這里就不贅述啦。
想復(fù)習(xí)的童鞋可以看這里:Java的接口總結(jié)
本文主要是想記錄一下接口的細(xì)節(jié)問題以及JDK1.8新增的一些知識(shí)。
1.接口的分類
- 普通的接口;
- 標(biāo)記接口,不含任何方法的接口,
- 典型的就是:Cloneable接口
- 函數(shù)式接口:每個(gè)函數(shù)式接口只含一個(gè)抽象方法,主要是針對(duì)于lambda表達(dá)式而設(shè)計(jì)的;在對(duì)應(yīng)的接口上加入@functionalInterface
2.接口的新特性
在JDK 1.8,我們可以在接口中添加兩種非抽象的方法實(shí)現(xiàn):
- default關(guān)鍵字開頭的,默認(rèn)方法;
- static關(guān)鍵字開頭的,靜態(tài)方法;
我們可以從1.8加入的Predicate接口的源碼觀察到這一點(diǎn):
package java.util.function;
import java.util.Objects;
/**
* Represents a predicate (boolean-valued function) of one argument.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #test(Object)}.
*
* @param <T> the type of the input to the predicate
*
* @since 1.8
*/
@FunctionalInterface 函數(shù)式接口
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
/**
* Returns a composed predicate that represents a short-circuiting logical
* AND of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code false}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ANDed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* AND of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default Predicate<T> and(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
/**
* Returns a predicate that represents the logical negation of this
* predicate.
*
* @return a predicate that represents the logical negation of this
* predicate
*/
default Predicate<T> negate() {
return (t) -> !test(t);
}
/**
* 默認(rèn)方法實(shí)現(xiàn)
* Returns a composed predicate that represents a short-circuiting logical
* OR of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code true}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ORed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* OR of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
default Predicate<T> or(Predicate<? super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
/**
* 靜態(tài)方法實(shí)現(xiàn)
* Returns a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}.
*
* @param <T> the type of arguments to the predicate
* @param targetRef the object reference with which to compare for equality,
* which may be {@code null}
* @return a predicate that tests if two arguments are equal according
* to {@link Objects#equals(Object, Object)}
*/
static <T> Predicate<T> isEqual(Object targetRef) {
return (null == targetRef)
? Objects::isNull
: object -> targetRef.equals(object);
}
}
3.易錯(cuò)點(diǎn)
再次強(qiáng)調(diào)一遍:接口不可以被實(shí)例化,即不能A a = new A()
在面試過程中,我提問面試者該問題時(shí),答錯(cuò)or不確定者不在少數(shù)。深入了解才發(fā)現(xiàn),很多人沒有弄清楚匿名內(nèi)部類
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println("匿名內(nèi)部類");
}
};
很多人會(huì)拿這個(gè)栗子來反駁我說,其實(shí)這是匿名內(nèi)部類的寫法,等價(jià)于創(chuàng)建一個(gè)class,然后implements這個(gè)接口;