Lab 8: 泛型(Generics) 
本次 Lab 时长为 2  周,DDL 为 11 月 13 日 23:59 。
 
实验目的 
实验题目 
Question 1 简答题 
简述 ArrayList 和 LinkedList 的区别。
Question 2 简答题 
阅读下面的程序,写出它的输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public  class  Animal  {    public  Animal ()  {         System.out.println("I am an animal" );     } } public  class  Dog  extends  Animal  {    public  Dog ()  {         System.out.println("I am a dog" );     } } public  class  AnimalTest  {    public  <T, S extends  T > T testDemo (T t, S s)  {         System.out.println("I am type T and my type is "  + t.getClass().getName());         System.out.println("I am type S and my type is "  + s.getClass().getName());         return  t;     }     public  static  void  main (String[] args)  {         AnimalTest  test  =  new  AnimalTest ();         Dog  dog  =  new  Dog ();         Animal  animal  =  new  Animal ();         Animal  animal1  =  test.testDemo(animal, dog);     } } 
 
Question 3 简答题 
阅读下面的程序,写出它的输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public  class  Animal  {    public  Animal ()  {         System.out.println("I am an animal" );     } } public  class  Dog  extends  Animal  {    public  Dog ()  {         System.out.println("I am a dog" );     } } import  java.util.List;import  java.util.ArrayList;public  class  AnimalTest1  {    public  void  testDemo (List<?> s)  {         for  (Object obj : s) {             System.out.println("My type is "  + obj.getClass().getName());         }     }     public  static  void  main (String[] args)  {         AnimalTest1  test  =  new  AnimalTest1 ();         Dog  dog  =  new  Dog ();         Animal  animal  =  new  Animal ();         List<Animal> s = new  ArrayList <Animal>();         s.add(dog);         s.add(animal);         test.testDemo(s);     } } 
 
Question 4:编程题 
请用 LinkedList 实现一个支持泛型的栈 MyStack,并在 main 函数中测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import  java.util.LinkedList;public  class  MyStack <T> {    private  LinkedList<T> values = new  LinkedList <T>();     public  void  push (T t)  {              }     public  T pop ()  {              }     public  T top ()  {              }     public  static  void  main (String[] args)  {              } } 
 
关于泛型 
想要完全掌握泛型,Lab 里提供的示例是远远不够的,大家需要自行寻找一些资料进行学习。
题外话,泛型不一定只能叫做 E、T、V 等,和普通标识符一样,它可以是任何名字,比如 TElement,TValue。😉