SCJP題庫第192題
Given:
3. import java.util.*;
4. public class G1{
5. public void takeList(List<? extends String> list){
6. //insert code here
7. }
8. }
Which three code fragments, inserted independently at line 6, will compile? (Choose three.)
A. list.add("foo");
B. Object o = list;
C. String s = list.get(0);
D. list = new ArrayList<String>();
E. list = new ArrayList<Object>();
Ans: BCD
解說:
list是指向一個放置String與其子類別型態的集合物件
B是ok的,因此任何物件皆可用Object型態的參考存取
list.get(0)傳回集合物件中第0個位置的物件,其型態是String或其子類別
list = new ArrayList<String>(); list指向內存String物件的集合物件,符合list的型態宣告。
Comments