Given:
public interface Moveable<Integer> {
public default void walk(Integer distance) {
System.out.println("Walking");
}
public void run(Integer distance);
}
Which statement is true?
A. Moveable can be used as below:
Moveable<Integer> animal = n -> System.out.println("Running" + n);
animal.run(100);
animal.walk(20);
B. Moveable can be used as below:
Moveable<Integer> animal = n -> n + 10;
animal.run(100);
animal.walk(20);
C. Moveable can be used as below:
Moveable animal = (Integer n) -> System.out.println(n);
animal.run(100);
Moveable.walk(20);
D. Movable cannot be used in a lambda expression.
題解
Moveable介面需要實作的方法為僅接受傳入一個整數參數,且沒有回傳值的的run方法。walk方法是Moveable介面的預設方法,只能在實體化出Moveable介面的物件後才能使用,無法直接使用Moveable介面來呼叫。
選項A,正確。
選項B,run方法不能有回傳值。
選項C,walk方法不能用這樣的方式呼叫。
選項D,Moveable介面可以使用Lambda表示式來實作。