
Java interface 中的 default 关键字
default关键字在Java中用于接口中的方法定义,并不是访问修饰符。它允许你在接口中提供方法的默认实现。这意味着,如果一个类实现了这个接口,但没有提供该方法的具体实现,那么这个类可以直接使用接口中提供的默认实现。
default关键字仅适用于接口!
简言之,
default关键字提供了接口中方法的默认实现
default 方法的特点:
提供默认实现:
default方法允许接口提供方法的默认实现,这样实现接口的类可以选择性地覆盖这些方法。如果没有default关键字,接口中的所有方法都是抽象的,即它们没有实现,要求实现接口的类必须提供具体的实现。增强接口的能力而不破坏现有代码:在Java 8之前,向接口添加新方法会导致所有实现该接口的类都需要更新以实现新方法,否则将导致编译错误。
default方法解决了这个问题,因为它允许向接口添加新的功能而不会破坏现有的实现。扩展性:
default方法使得接口可以在不强制其实现类更新的情况下进行功能扩展。这对于库和框架的维护者来说特别有用,因为他们可以为接口添加新功能,同时保持与旧版本的兼容性。不是访问修饰符:需要注意的是,
default并不是访问修饰符(如public,protected,private)。实际上,接口中的所有方法默认都是public的,包括那些带有default关键字的方法。
例如,在Consumer<T>接口中:
package java.util.function;
import java.util.Objects;
/**
* Represents an operation that accepts a single input argument and returns no
* result. Unlike most other functional interfaces, {@code Consumer} is expected
* to operate via side-effects.
*
* <p>This is a <a href="package-summary.html">functional interface</a>
* whose functional method is {@link #accept(Object)}.
*
* @param <T> the type of the input to the operation
*
* @since 1.8
*/
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
/**
* Returns a composed {@code Consumer} that performs, in sequence, this
* operation followed by the {@code after} operation. If performing either
* operation throws an exception, it is relayed to the caller of the
* composed operation. If performing this operation throws an exception,
* the {@code after} operation will not be performed.
*
* @param after the operation to perform after this operation
* @return a composed {@code Consumer} that performs in sequence this
* operation followed by the {@code after} operation
* @throws NullPointerException if {@code after} is null
*/
default Consumer<T> andThen(Consumer<? super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
}
