韩顺平 java笔记 第25讲 泛型

时间:2021-05-16 21:05:41

1.    ArrayList<Dog> al = new ArrayList<Dog>();

  Dog dog1 = new Dog();

  al.add(dog1);

  Dog temp = al.get(0);

    ArrayList  al = new ArrayList();

    Dog dog1 = new Dog();

    al.add(dog1);

    Dog temp = (Dog)al.get(0);

2.给T什么类型就是什么类型

  class Gen<T>{

    private T o;

    public Gen(T a){

      o=a;

    }

    public void showTypeName(){

      System.out.println("类型是:"+o.getClass.getName());

    }

  }

   Gen <String> gen1 = new Gen<String>("aaa");//给T  String类型

     //Gen<Bird> gen1 = new Gen<Bird>(new Bird());

   gen1.show.TypeName();//打印出String类型