1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| // 简单范型 class Test<T> { private T var; public T getVar() { return var; } public void setVar(T var) { this.var = var; } } // 多元泛型 class TestKV<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; } }
public class Demo { public static void main(String[] args) { Test<String> test = new Test<String>(); test.setVar("aa"); System.out.println(test.getVar());
TestKV<String, String> testKV = new TestKV<String, String>(); testKV.setKey("hello"); testKV.setValue("world"); System.out.println(testKV.getKey() +":"+testKV.getValue()); } }
|