Java汎用の単純なタイプのマルチパラメータタイプ
1940 ワード
package com.study.generics;
/**
*
* @author Administrator
* @description ,
* @param <K>
* @param <V>
*/
class GenericsClassTwo<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;
}
/**
* toString ,
*/
public String toString() {
return (" :"+this.key + " "+this.value);
}
}
public class GenericsDemo02 {
public static void main(String []args) {
// , , 。
// Object equals hashCode 。 Compareable compareTo() ,
// 。
// , 。( , )
GenericsClassTwo<String,Integer> twoGenerics= new GenericsClassTwo<String,Integer>();
twoGenerics.setKey("1");
twoGenerics.setValue(100);
System.out.println(" :"+twoGenerics.getKey());
System.out.println(" :"+twoGenerics.getValue());
twoGenerics.setKey("1");
twoGenerics.setValue(100);
System.out.println(" :"+twoGenerics.getKey());
System.out.println(" :"+twoGenerics.getValue());
GenericsClassTwo<String,Float> twoGenerics1= new GenericsClassTwo<String,Float>();
twoGenerics1.setKey("1");
twoGenerics1.setValue(339.33f);
System.out.println(" :"+twoGenerics1.getKey());
System.out.println(" :"+twoGenerics1.getValue());
}
}