新視野——Java的接口

對(duì)于Java系程序猿而言,大家對(duì)于接口一定不會(huì)陌生,畢竟經(jīng)常與之打交道的。我們對(duì)于Java接口的功能和使用這里就不贅述啦。

想復(fù)習(xí)的童鞋可以看這里:Java的接口總結(jié)

本文主要是想記錄一下接口的細(xì)節(jié)問題以及JDK1.8新增的一些知識(shí)。

1.接口的分類

  1. 普通的接口;
  2. 標(biāo)記接口,不含任何方法的接口,
  3. 函數(shù)式接口:每個(gè)函數(shù)式接口只含一個(gè)抽象方法,主要是針對(duì)于lambda表達(dá)式而設(shè)計(jì)的;在對(duì)應(yīng)的接口上加入@functionalInterface

2.接口的新特性

在JDK 1.8,我們可以在接口中添加兩種非抽象的方法實(shí)現(xiàn):

  1. default關(guān)鍵字開頭的,默認(rèn)方法;
  2. 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è)接口;

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容