A programmer must create a generic class MinMax and the type parameter of MinMax must implement
Comparable. Which implementation of MinMax will compile?
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 */}
答案:A
參考:12-3 泛型
-----------------------------------------------------
一個程式設計師想要做一個帶有泛型的類別 MinMax ,而且屬性想要用泛型且去實做Comparable,
照理說實做要用implement ,但在泛型不論是實做或繼承父介面一律用extends ,不可以用implement
沒有留言:
張貼留言