本文共 5254 字,大约阅读时间需要 17 分钟。
/** 002 * @author Rollen-Holt 使用泛型 003 */ 004 005 class hello<T, V> { 006 hello(){ 007 008 } 009 010 public T getName(){ 011 return name; 012 } 013 014 public void setName(T name){ 015 this.name = name; 016 } 017 018 public V getAge(){ 019 return age; 020 } 021 022 public void setAge(V age){ 023 this.age = age; 024 } 025 026 private T name; 027 private V age; 028 029 public static void main(String[] args){ 030 hello<String, Integer> he = new hello<String, Integer>(); 031 he.setAge(10); 032 he.setName("Rollen Holt"); 033 System.out.println(he.getName() + " " + he.getAge()); 034 } 035 } 036 037 /** 038 * @author Rollen-Holt 泛型类的构造方法定义 039 */ 040 041 class hello<T, V> { 042 043 hello(T name, V age){ 044 this.age = age; 045 this.name = name; 046 } 047 048 public T getName(){ 049 return name; 050 } 051 052 public V getAge(){ 053 return age; 054 } 055 056 private T name; 057 private V age; 058 059 public static void main(String[] args){ 060 hello<String,Integer> he=new hello<String,Integer>("Rollen",12); 061 System.out.println(he.getName()+" "+he.getAge()); 062 } 063 } 064 065 /** 066 * @author Rollen-Holt 使用通配符 067 */ 068 069 class info<T> { 070 info(T name){ 071 this.name = name; 072 } 073 private T name; 074 } 075 076 class hello{ 077 public static void function(info<?> temp){ 078 System.out.println("内容: "+temp); 079 } 080 081 public static void main(String[] args){ 082 info<String> demo=new info<String>("Rollen"); 083 function(demo); 084 } 085 } 086 087 /** 088 * @author Rollen-Holt 泛型上限 089 */ 090 091 class info<T>{ 092 info(T age){ 093 this.age=age; 094 } 095 private T age; 096 } 097 098 class hello{ 099 public static void function(info<? extends Number> temp){ 100 System.out.println("内容"+ temp); 101 } 102 103 public static void main(String[] args){ 104 105 info<Integer> demo=new info<Integer>(1); 106 function(demo); 107 } 108 } 109 110 /** 111 * @author Rollen-Holt 泛型下限 112 */ 113 114 class info<T>{ 115 info(T age){ 116 this.age=age; 117 } 118 private T age; 119 } 120 121 class hello{ 122 public static void function(info<? super String> temp){ 123 System.out.println("内容"+ temp); 124 } 125 126 public static void main(String[] args){ 127 128 // 此处只能使用String 或者Object 129 info<String> demo=new info<String>("Rollen"); 130 function(demo); 131 } 132 } 133 /** 134 * @author Rollen-Holt 泛型和子类继承的限制 135 */ 136 137 class info<T>{ 138 } 139 140 class hello{ 141 public static void main(String[] args){ 142 info<String> demo1=new info<String>(); 143 info<Object> demo2=new info<Object>(); 144 //demo2=demo1; 此处错误 145 146 } 147 } 148 149 /** 150 * 上面的例子说明,一个类的子类可以通过多态性被其父类实例化 151 * 但是在泛型操作中,子类的泛型类型是无法被其父类的泛型类型实例化的。 152 */ 153 如果允许上面的条语句的话,会出现: 154 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 155 Type mismatch: cannot convert from info<String> to info<Object> 156 157 at hello.main(hello.java:12) 158 泛型接口的两种实现: 159 /** 160 * @author Rollen-Holt 泛型接口的实现1 161 */ 162 163 interface info<T> { 164 public void say(); 165 } 166 167 // 直接在子类之后声明泛型 168 class hello<T> implements info<T>{ 169 170 public static void main(String[] args){ 171 info<String> demo = new hello<String>(); 172 demo.say(); 173 174 } 175 176 public void say(){ 177 System.out.println("hello"); 178 179 } 180 } 181 182 /** 183 * @author Rollen-Holt 泛型接口的实现2 184 */ 185 186 interface info<T> { 187 public void say(); 188 } 189 190 // 在子类实现的接口中明确给出泛型类型 191 class hello implements info<String>{ 192 193 public static void main(String[] args){ 194 info<String> demo = new hello(); 195 demo.say(); 196 197 } 198 199 public void say(){ 200 System.out.println("hello"); 201 202 } 203 } 204 /** 205 * @author Rollen-Holt 使用泛型通一传入参数的类型 206 */ 207 208 class info<T> { 209 private T var; 210 211 public T getVar(){ 212 return var; 213 } 214 215 public void setVar(T var){ 216 this.var = var; 217 } 218 public String toString(){ 219 return this.var.toString(); 220 } 221 222 } 223 class hello{ 224 public static void main(String[] args){ 225 info<String> demo1=new info<String>(); 226 info<String> demo2=new info<String>(); 227 demo1.setVar("Rollen"); 228 demo2.setVar("Holt"); 229 printAdd(demo1, demo2); 230 } 231 // 此处传递的都是同一个String类型的 232 public static <T> void printAdd(info<T> demo1, info<T> demo2){ 233 System.out.println(demo1.getVar()+" "+demo2.getVar()); 234 } 235 } 236 否则的话如下所示:出现错误: 237 /** 238 * @author Rollen-Holt 使用泛型通一传入参数的类型 239 */ 240 241 class info<T> { 242 private T var; 243 244 public T getVar(){ 245 return var; 246 } 247 248 public void setVar(T var){ 249 this.var = var; 250 } 251 public String toString(){ 252 return this.var.toString(); 253 } 254 255 } 256 class hello{ 257 public static void main(String[] args){ 258 info<String> demo1=new info<String>(); 259 info<Integer> demo2=new info<Integer>(); 260 demo1.setVar("Rollen"); 261 demo2.setVar(30); 262 //此处调用错误 263 printAdd(demo1, demo2); 264 } 265 // 此处传递的都是同一个String类型的 266 public static <T> void printAdd(info<T> demo1, info<T> demo2){ 267 System.out.println(demo1.getVar()+" "+demo2.getVar()); 268 } 269 } 270 271 Exception in thread "main" java.lang.Error: Unresolved compilation problem: 272 The method printAdd(info<T>, info<T>) in the type hello is not applicable for the arguments (info<String>, info<Integer>) 273 274 at hello.main(hello.java:26)转载地址:http://dnlmi.baihongyu.com/