Java泛型笔记 发表于 2017-01-26 | 更新于 2017-02-21 | 评论数: 本文字数: 784 | 阅读时长 ≈ 1 分钟123456789101112131415161718192021222324252627class Notepad<K,V>{ // 此处指定了两个泛型类型 private K key ; // 此变量的类型由外部决定 private V value ; // 此变量的类型由外部决定 public K getKey(){ return this.key ; } public V getValue(){ return this.value ; } public void setKey(K key){ this.key = key ; } public void setValue(V value){ this.value = value ; }};public class GenericsDemo01{ public static void main(String args[]){ Notepad<String,Integer> t = null ; // 定义两个泛型类型的对象 t = new Notepad<String,Integer>() ; // 里面的key为String,value为Integer t.setKey("Not Found") ; // 设置第一个内容 t.setValue(18) ; // 设置第二个内容 System.out.print("姓名;" + t.getKey()) ; // 取得信息 System.out.print(",年龄;" + t.getValue()) ; // 取得信息 }};