關於泛型? extends T和? super T

https://www.cnblogs.com/zhaoyibing/p/9051428.html

public class TestArrayList {
   public static void main(String[] args) {
       List<? super Animal> animal = new ArrayList<Animal>();
       animal.add( new Dog() );
       //Object a= animal.get( 0 );
       Dog a1 = (Dog) animal.get( 0 );
       a1.dog();
測試狗

Process finished with exit code 0
<? extends T>只能讀取無法儲存。 <? super T> 能儲存,並讀取,只是讀取時會喪失原類型,需要強轉。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章