SCJP 1.6版考題 207
A programmer must create a generic class MinMax and the type parameter of MinMax must implement Comparable.
一個程式設計必須要建立一個泛用類別
MinMax ,MinMax類別的型態參數必須實作Comparable介面,
Which implementation of MinMax will compile?
下面那一個MinMax的實作能被編譯?
A. class MinMax<E extends Comparable<E>>{
E min = null;
E max = null;
public MinMax(){}
public void put(E value){/* store min or max */}
B. class MinMax<E implements Comparable<E>>{
E min = null;
E max = null;
public MinMax(){}
public void put(E value){/* store min or max */}
C. class MinMax<E extends Comparable<E>>{
<E> E min = null;
<E> E max = null;
public MinMax(){}
public <E> void put(E value){/* store min or max */}
D. class MinMax<E implements Comparable<E>>{
<E> E min = null;
<E> E max = null;
public MinMax(){}
public <E> void put(E value){/* store min or max */}
解答
Ans: A
解說: 參考泛型的寫法。
|