亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

代碼
提交代碼
public class KeyValueGeneric<K,V> { // 把兩個泛型K、V定義在類上 /** * 類型為K的key屬性 */ private K key; /** * 類型為V的value屬性 */ 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 static void main(String[] args) { // 實例化對象,分別指定元素類型為整型、長整型 KeyValueGeneric<Integer, Long> integerLongKeyValueGeneric = new KeyValueGeneric<>(); // 調用setter、getter方法 integerLongKeyValueGeneric.setKey(200); integerLongKeyValueGeneric.setValue(300L); System.out.println("key=" + integerLongKeyValueGeneric.getKey()); System.out.println("value=" + integerLongKeyValueGeneric.getValue()); // 實例化對象,分別指定元素類型為浮點型、字符串類型 KeyValueGeneric<Float, String> floatStringKeyValueGeneric = new KeyValueGeneric<>(); // 調用setter、getter方法 floatStringKeyValueGeneric.setKey(0.5f); floatStringKeyValueGeneric.setValue("零點五"); System.out.println("key=" + floatStringKeyValueGeneric.getKey()); System.out.println("value=" + floatStringKeyValueGeneric.getValue()); } }
運行結果