1 /*****************
2 ***第08章 泛型
3 *******知识点:
4 **************1.泛型定义
5 **************2.泛型使用
6 ******************2.1 普通泛型
7 ******************2.2 类型通配符
8 ******************2.3 受限泛型
9 ******************2.4 泛型接口、类、方法
10 ******************2.5 泛型数组
11 ******************2.6 嵌套设置
12 */
13 import java.util.*;
14
15 class Point<T>{ // 此处可以随便写标识符号,T是type的简称
16 private T var ; // var的类型由T指定,即:由外部指定
17 public T getVar(){ // 返回值的类型由外部决定
18 return var ;
19 }
20 public void setVar(T var){ // 设置的类型也由外部决定
21 this.var = var ;
22 }
23 public Point(T t){
24 this.var = t;
25 }
26 public Point(){
27
28 }
29 }
30
31 class PointKeyValue<K,V>{ // 此处指定了两个泛型类型
32 private K key; // 此变量的类型由外部决定
33 private V value; // 此变量的类型由外部决定
34 public void setKey(K key){
35 this.key = key;
36 }
37 public void setValue(V value){
38 this.value = value;
39 }
40 public K getKey(){
41 return key;
42 }
43 public V getValue(){
44 return value;
45 }
46
47 public String toString(){
48 return "{" + key + "," + value + "}";
49 }
50 public PointKeyValue(K k ,V v){
51 this.key = k;
52 this.value = v;
53 }
54 public PointKeyValue(){
55
56 }
57
58 }
59
60 class PointInner<T,K,V>{
61 private PointKeyValue p_PointKeyValue; // 接收两个泛型类型成员变量
62 private Point p_Point;
63 public PointInner(T t,K k,V v){
64 this.p_Point = new Point(t);
65 this.p_PointKeyValue = new PointKeyValue(k,v);
66 }
67
68 public String toString(){
69 return "{" + p_Point.getVar() + ":" + p_PointKeyValue.getKey() + "," + p_PointKeyValue.getValue() + "}";
70 }
71 }
72
73 public class test8{
74 public static void main(String[] args){
75 showDemo("泛型定义");
76 testGenericsDefined();//演示泛型定义
77
78 showDemo("泛型使用");
79 testGenericsUse();//演示泛型使用
80 }
81
82 /*
83 *1.泛型定义
84 */
85 public static void testGenericsDefined(){
86 /*List l_Gen = new ArrayList();
87 l_Gen.add(1);
88 l_Gen.add(2);
89 l_Gen.add("aaaaa");//注意跟之前两个元素类型发生了变化,但不报错
90 System.out.println("集合中元素有:" + l_Gen);
91
92 List<Integer> l_Gen1 = new ArrayList<Integer>();
93 l_Gen1.add(1);
94 l_Gen1.add(2);
95 l_Gen1.add("aaaaa");//此行报错
96 System.out.println("集合中元素有:" + l_Gen1);*/
97
98 //泛型:Java的参数化类型,即允许我们在创建集合时指定集合中元素的类型,且该集合只能存储指定类型的元素
99 //使用范围:定义类、接口、方法是使用类型形参
100 //需注意:静态初始化块中是不允许的
101
102 }
103
104 /*
105 *2.泛型使用
106 */
107 public static void testGenericsUse(){
108 /**2.1 普通泛型**/
109 System.out.println("演示2.1 普通泛型===========");
110 Point<Integer> p_Generics = new Point<Integer>(); //里面的var类型为Integer
111 p_Generics.setVar(new Integer(1));//设置var
112 System.out.println("返回int值:" + (p_Generics.getVar().intValue()));//返回var
113
114 PointKeyValue<Integer,String> p_KeyValue = new PointKeyValue<Integer,String>();
115 p_KeyValue.setKey(new Integer(10));
116 p_KeyValue.setValue("Ciade");
117 System.out.println("返回key:" + p_KeyValue.getKey());
118 System.out.println("返回value:" + p_KeyValue.getValue());
119 System.out.println("返回p_KeyValue:" + p_KeyValue.toString());
120
121 /**2.2 类型通配符**/
122 System.out.println("演示2.2 类型通配符===========");
123 PointKeyValue<Integer,Double> p_PointKeyValue2 = new PointKeyValue<Integer,Double>();
124 PointKeyValue<String,String> p_PointKeyValue3 = new PointKeyValue<String,String>();
125
126 p_PointKeyValue2.setKey(new Integer(2));
127 p_PointKeyValue2.setValue(new Double(2.2));
128
129 p_PointKeyValue3.setKey("3");
130 p_PointKeyValue3.setValue("33333333");//注意区分p_PointKeyValue2和p_PointKeyValue3
131
132 demoWildcardFun(p_PointKeyValue2);//调用通配符泛型演示方法
133 demoWildcardFun(p_PointKeyValue3);//调用通配符泛型演示方法
134
135 /**2.3 受限泛型**/
136 System.out.println("演示2.3 受限泛型===========");
137 PointKeyValue<Integer,String> p_PointKeyValue4 = new PointKeyValue<Integer,String>();
138
139 p_PointKeyValue4.setKey(new Integer(4));
140 p_PointKeyValue4.setValue("4444444");
141
142 //demoLimitedFun(p_PointKeyValue2);//这行报错 类型受限
143 //demoLimitedFun(p_PointKeyValue3);//这行报错 类型受限
144 demoLimitedFun(p_PointKeyValue4);
145
146 /**2.4 泛型接口、类、方法**/
147 //接口、类 、方法 参照上面的Point类和PointKeyValue类的定义使用
148 //温馨提示 当重载方法时,若重载的参数个数相同,参数类型和返回值不同时 可以使用泛型方法
149
150 /**2.5 泛型数组**/
151 //Java 不支持泛型数组。也就是说
152 //List<String>[] ls = new ArrayList<String>[10]; //编译报错
153 //而你可以这样子
154 //List<String>[] ls = new ArrayList[10];
155 //不过不建议使用
156
157 /**2.6 嵌套设置**/
158 System.out.println("演示2.6 嵌套设置===========");
159 PointInner<Integer,Integer,String> p_PointInner = new PointInner<Integer,Integer,String>
160 (new Integer(7),new Integer(7),"777777777");
161 System.out.println("PointInner对象:" + p_PointInner);
162
163 }
164
165 /*
166 * 通配符泛型演示方法
167 */
168 public static void demoWildcardFun(PointKeyValue<?,?> temp){ // 可以接收任意的泛型对象 其中?为通配符
169 System.out.print("返回key:" + temp.getKey());
170 System.out.print("_返回value:" + temp.getValue());
171 System.out.println("_返回PointKeyValue对象:" + temp.toString());
172
173 }
174
175 /*
176 * 受限泛型演示方法
177 */
178 //第一个参数只能接收Number及其Number的子类,第二个参数只能接收String或Object类型的泛型
179 public static void demoLimitedFun(PointKeyValue<? extends Number,? super String> temp){
180 demoWildcardFun(temp);
181 }
182
183
184 /*
185 * 抽取打印演示函数 用于打印演示功能提示
186 */
187 public static void showDemo(String demoStr){
188 System.out.println("演示:" + demoStr);
189 }
190 }