飞猫xxx

导航

 

 

java 泛型学习笔记

1、泛型——所谓泛型是指对象建立时不指定类中属性的具体类型,而由外部在声明及实例化对象时指定类型。

2、泛型可以解决数据类型的安全性问题,其主要原理是在类声明时通过一个标识表示类中某个属性的类型或者是某个方法的返回值及参数类型。

*声明泛型

 1 class Point<T>
 2 {
 3     private T var;            //此变量的类型由外部指定
 4     public T getVar()        //返回值的类型由外部指定
 5     {
 6         return var;
 7     }
 8     public void setVar(T var)    //设置的类型由外部指定
 9     {
10         this.var = var;
11     }
12 }
13 
14 public class GenericsDemo05
15 {
16     public static void main(String[] args)
17     {
18         Point<Integer> p = new Point<Integer>();  //var类型设置为Integer
19         
20         p.setVar(30);                                //设置数字,自动装箱
21         System.out.println(p.getVar());
22     }
23 }

2、设置多个泛型类型

如果一个类中有多个属性需要使用不同的泛型声明,可以在声明类时指定多个泛型类型。

 1 class Notepad<K,V>        //指定两个泛型类型
 2 {
 3     private K key;        //此变量的类型由外部决定
 4     private V value;    //此变量的类型由外部决定
 5     public K getKey()
 6     {
 7         return key;
 8     }
 9     public void setKey(K key)        //设置key
10     {
11         this.key = key;
12     }
13     public V getValue()
14     {
15         return value;
16     }
17     public void setValue(V value)        //设置value
18     {    
19         this.value = value;
20     }
21 }
22 
23 public class GenericsDemo
24 {
25     public static void main(String[] args)
26     {
27         Notepad<String,Integer> t = null;        //定义两个泛型类型的对象
28         t = new Notepad<String,Integer>();        //里面的key为String,value为Integer
29     
30         t.setKey("张三");
31         t.setValue("30");
32         System.out.print("姓名:" + t.getKey());
33         System.out.print(",年龄:" + t.getValue());
34     }
35 }

3、通配符

4、泛型的上下限

5、泛型接口

*范例:定义泛型接口

1 interface Info<T>
2 {
3     public T getVar();
4 }

*泛型接口的两种实现方式

|——直接在子类后声明泛型

|——直接在子类实现的接口中明确地给出泛型类型

*在子类的定义上声明泛型类型

 1 interface Info<T>
 2 {
 3     public T getVar();
 4 }
 5 class InfoImpl<T> implements Info<T>
 6 {
 7     private T var;
 8     public InfoImpl(T var)
 9     {
10         this.setVar(var);
11     }
12     public void setVar(T var)
13     {
14         this.var = var;
15     }
16     public T getVar()
17     {
18         return this.var;
19     }
20 }
21 
22 public class GenericsDemo
23 {
24     public static void main(String[] args)
25     {
26         Info<String> i = null;
27         i = new InfoImple<String>("张三");
28         System.out.println("内容: " + i.getVar());
29     }
30 }

*在接口中指定具体类型

 1 interface Info<T>
 2 {
 3     public T getVar();
 4 }
 5 class InfoImpl implements Info<String>
 6 {
 7     private String var;
 8     public InfoImpl(String var)
 9     {
10         this.setVar(var);
11     }
12     public void setVar(String var)
13     {
14         this.var = var;
15     }
16     public String getVar()
17     {
18         return this.var;
19     }
20 }
21 
22 public class GenericsDemo
23 {
24     public static void main(String[] args)
25     {
26         Info<String> i = null;
27         i = new InfoImple("张三");
28         System.out.println("内容: " + i.getVar());
29     }
30 }

 6.泛型方法

*可以在类中定义泛型方法,所在的类可以是泛型类,也可以不是

 1 class Demo
 2 {
 3     public <T> T fun(T t)
 4     {
 5         return t
 6     }
 7 }
 8 public class GenericsDemo
 9 {
10     public static void main(String[] args)
11     {
12         Demo d = new Demo();
13         String str = d.fun("张三");
14         int i = d.fun(30);
15         System.out.println(str);
16         System.out.println(i);
17     }
18 }

 7、总结

——泛型可以使程序的操作更加安全,可以避免类转换异常

——在程序中如果使用类时没有指定泛型,则泛型将被擦掉,将使用Object接收参数。

——可以使用通配符”?“接收全部的泛型类型对象

——通过<? extends 类>可以设置泛型的上限,通过<? super 类>可以设置泛型的下限

——泛型方法可以定义在泛型类中,也可以定义在普通类中

——泛型可以在接口中定义,实现泛型接口的子类要指明具体的泛型类型。

——泛型可以嵌套使用

 

 

 

posted on 2013-08-15 18:47  飞猫xxx  阅读(252)  评论(0)    收藏  举报