泛型与数据类型转换

          jdk6都出来有一点时间,感觉自己还是停留在jdk4的水平上,今天研究线程的时候,看到资料上的
         ExecutorService pool = Executors.newCachedThreadPool();以及class MatchCounter implements Callable<Integer> ,感觉很是奇怪.后来上网找了资料才知道是 Jdk5.0新特性——Generic Types (泛型).
        我就将我理解的大体说一下,大家学习一下.
None.gif import  Java.util.Hashtable; 
ExpandedBlockStart.gifContractedBlock.gif
class  Test  dot.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif  
public static void main(String[] args) dot.gif
InBlock.gif   Hashtable h 
= new Hashtable(); 
InBlock.gif   h.put(
"key""value"); 
InBlock.gif   String s 
= (String)h.get("key"); 
InBlock.gif   System.out.println(s); 
ExpandedSubBlockEnd.gif  }
 
ExpandedBlockEnd.gif}
 
      这个我们做了类型转换,是不是感觉很烦的,并且强制类型转换会带来潜在的危险,系统可能会抛一个 ClassCastException 异常信息。在 JDK5.0 中我们完全可以这么做,如:
None.gif import  java.util.Hashtable;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  Test  dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        Hashtable
<String, Integer> h = new Hashtable<String, Integer>();
InBlock.gif        h.put(
"key"new Integer(123));
InBlock.gif        
int s = h.get("key");
InBlock.gif        System.out.println(s);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

      这里我们使用泛化版本的 HashMap, 这样就不用我们来编写类型转换的代码了,类型转换的过程交给编译器来处理,是不是很方便,而且很安全。上面是 String 映射到 String ,也可以将 Integer 映射为 String ,只要写成 HashTable<Integer,String> h=new HashTable<Integer,String>();h.get(new Integer(0)) 返回 value 。果然很方便!

      从上面有没有看到有点别扭啊,h.get(new Integer(123))这里的new Integer(123);好烦的,在JDK5.0之前我们只能忍着了,现在这种问题已经解决了,请看下面这个方法。我们传入一个int这一基本型别,然后再将i的值直接添加到List中,其实List是不能储存基本型别的,List中应该存储对象,这里编译器将int包装成Integer,然后添加到List中去。接着我们用List.get(0);来检索数据,并返回对象再将对象解包装成int。恩,JDK5.0给我们带来更多方便与安全。
ExpandedBlockStart.gif ContractedBlock.gif public   void  autoBoxingUnboxing( int  i)  dot.gif
InBlock.gif  ArrayList
<Integer> L= new ArrayList<Integer>(); 
InBlock.gif  L.add(i); 
InBlock.gif  
int a = L.get(0); 
InBlock.gif  System.out.println(
"The value of i is " + a); 
ExpandedBlockEnd.gif}
 

      也许你已经发现在 TestGen<K,V> 这个泛型类 , 其中 K,V 可以是任意的型别。也许你有时候呢想限定一下 KV 当然范围,怎么做呢?看看如下的代码:
None.gif package  jdk5;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  TestGen2 < extends  String, V  extends  Number >   dot.gif {
InBlock.gif    
private V v = null;
InBlock.gif
InBlock.gif    
private K k = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setV(V v) dot.gif{
InBlock.gif        
this.v = v;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public V getV() dot.gif{
InBlock.gif        
return this.v;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public void setK(K k) dot.gif{
InBlock.gif        
this.k = k;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public K getK() dot.gif{
InBlock.gif        
return this.k;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif        TestGen2
<String, Integer> t2 = new TestGen2<String, Integer>();
InBlock.gif        t2.setK(
new String("String"));
InBlock.gif        t2.setV(
new Integer(123));
InBlock.gif        System.out.println(t2.getK());
InBlock.gif        System.out.println(t2.getV());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

上边K的范围是<=String V的范围是<=Number,注意是“<=”,对于K可以是String的,V当然也可以是Number,也可以是Integer,Float,Double,Byte等。
  这个是单一的限制,你也可以对型别多重限制,如下: 

         class C<T extends Comparable<? super T> & Serializable>
  我们来分析以下这句,T extends Comparable这个是对上限的限制,Comparable< super T>这个是下限的限制,Serializable是第2个上限。一个指定的类型参数可以具有一个或多个上限。具有多重限制的类型参数可以用于访问它的每个限制的方法和域。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值