- class GenericDemo5
- {
- public static void main(String[] args)
- {
- /*
- Demo<String> d=new Demo<String>();
- d.show("ss");
- d.print("asd");
-
- Demo<Integer> d1= new Demo<Integer>();
- d1.show(4);
- */
- Demo2 d2=new Demo2();
- d2.show("fsd"); //为什么可以运行!!
- d2.show(1); //明明一个是 String 一个是 Integer
- d2.print(4);
- }
- }
- class Demo2 // 泛型定义在方法上:具有更大的方便性!
- // 不明白为什么没有不安全提示
- {
- public <T> void show (T t){
-
- System.out.println("Demo2 show:"+t);
- }
- public<Q> void print(Q q){
-
- System.out.println("Demo2 print:"+q);
- }
- }
复制代码 |
|