Which statement is true about the single abstract method of the java.util.function.Function interface?

A. It accepts one argument and returns void.
B. It accepts one argument and returns boolean.
C. It accepts one argument and always produces a result of the same type as the argument.
D. It accepts an argument and produces a result of any data type.

題解

Function 的 JDK 原始碼片段如下:

public interface Function<T, R> {
    R apply(T t);
}

選項 A,要回傳 R 型態的物件才對。

選項 B,要回傳 R 型態的物件才對。

選項 C,傳入的參數型態不一定要跟回傳型態一樣。

選項 D,正確。