泛型总结 - JAVA课堂笔记整理

总结
1泛型的产生意义,为了保证数据的安全性
2泛型的基本使用,由外部指定其具体的操作类型。
以后只要用到集合的地方,尽量使用泛型。
匹配任意类型的通配符

package com.cstp.connection;
***写一个泛型P.java***
public class P<T> {
 private T varT;
 public T getVarT(){
  return varT;
 }
 public void setVarT(T varT){
  this.varT = varT;
 }
}
********设置多个泛型命名为TestMapFan.java******
package com.cstp.connection;

public class TestMapFan<K,V> {
 private K key;
 private V value;
 public K getKey() {
  return key;
 }
 public void setKey(K key) {
  this.key = key;
 }
 public V getValue() {
  return value;
 }
 public void setValue(V value) {
  this.value = value;
 }
}
****测试泛型P.java和TestMapFan.java的类TestP.java*******
package com.cstp.connection;

public class TestP {
 public static void main(String[] args) {
  P<String> p = new P<String>();
  p.setVarT("asdf");
  p.getVarT();
  
  TestMapFan<Integer,String> tMap = new TestMapFan<Integer, String>();
  tMap.setKey(1);
  tMap.setValue("第一个字符串");
  System.out.println(tMap.getKey()+"--->"+tMap.getValue());
 }
}

posted @ 2011-11-19 15:42  中国聚龙  阅读(186)  评论(0编辑  收藏  举报