JAVA基础知识指导:限制泛型中类型参数的范围
也许你已经发现在code list 1中的TestGen这个泛型类,其中K,V可以是任意的型别。也许你有时候呢想限定一下K和V当然范围,怎么做呢?看看如下的代码://Code list 8
class TestGen2
{private V v=null;
private K k=null;
public void setV(V v){
this.v=v;}
public V getV(){
return this.v;}
public void setK(K k){
this.k=k; }
public V getK(){
return this.k; }
public static void main(String[] args)
{TestGen2t2=new TestGen2();
t2.setK(new String("String"));
t2.setV(new Integer(123));
System.out.println(t2.getK());
System.out.println(t2.getV());}}
页:
[1]