新视野——Java的接口

2018-03-07  本文已影响6人  顶级工程师闯天涯

对于Java系程序猿而言,大家对于接口一定不会陌生,毕竟经常与之打交道的。我们对于Java接口的功能和使用这里就不赘述啦。

想复习的童鞋可以看这里:Java的接口总结

本文主要是想记录一下接口的细节问题以及JDK1.8新增的一些知识。

1.接口的分类

  1. 普通的接口;
  2. 标记接口,不含任何方法的接口,
  3. 函数式接口:每个函数式接口只含一个抽象方法,主要是针对于lambda表达式而设计的;在对应的接口上加入@functionalInterface

2.接口的新特性

在JDK 1.8,我们可以在接口中添加两种非抽象的方法实现:

  1. default关键字开头的,默认方法;
  2. static关键字开头的,静态方法;

我们可以从1.8加入的Predicate接口的源码观察到这一点:


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 函数式接口
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);
    }

    /**
     * 默认方法实现
     * 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);
    }

    /**
    * 静态方法实现
     * 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.易错点

再次强调一遍:接口不可以被实例化,即不能A a = new A()

在面试过程中,我提问面试者该问题时,答错or不确定者不在少数。深入了解才发现,很多人没有弄清楚匿名内部类

 Runnable runnable = new Runnable() {
        @Override
        public void run() {
            System.out.println("匿名内部类");
        }
    };

很多人会拿这个栗子来反驳我说,其实这是匿名内部类的写法,等价于创建一个class,然后implements这个接口

上一篇 下一篇

猜你喜欢

热点阅读