package Demo; // 普通泛型 class Point<T> { // 此处可以随便写标识符号,T是type的简称 private T var; // var的类型由T指定,即:由外部指定 public T getVar() { // 返回值的类型由外部决定 return var; } public void setVar(T var) { // 设置的类型也由外部决定 this.var = var; } } public class GenericsDemo06 { public static void main(String[] args) { Point<String> p = new Point<String>(); // 里面的var类型为String类型 p.setVar("it"); // 设置字符串 System.out.println("Length Of String : " + p.getVar().length()); // 取得字符串的长度 } } package Demo; // 普通泛型 class Notepad<K, V> { // 此处指定了两个泛型类型 private K key; // 此变量的类型由外部决定 private V value; // 此变量的类型由外部决定 public K getKey() { return this.key; } public void setKey(K key) { this.key = key; } public V getValue() { return this.value; } public void setValue(V value) { this.value = value; } } public class GenericsDemo09 { public static void main(String[] args) { Notepad<Integer, String> t = null; // 定义两个泛型类型的对象 t = new Notepad<Integer, String>(); // 里面的key为Integer类型,value为String类型 t.setKey(99); // 设置第一个内容 t.setValue("it"); // 设置第二个内容 System.out.println("Key Of Integer : " + t.getKey() + " / Value Of String : " + t.getValue()); // 取得信息 } }