zds420 发表于 2013-2-3 12:54:48

Java 泛型之简单类型的多参数类型

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());}}
页: [1]
查看完整版本: Java 泛型之简单类型的多参数类型