当前位置: 代码迷 >> 综合 >> 泛型的几个较困惑的问题
  详细解决方案

泛型的几个较困惑的问题

热度:45   发布时间:2023-09-28 01:43:41.0
疑问点一:既然泛型在jvm中变成了原始类型,那么他最后是怎么转化为其他类型的呢,比如下面一段代码,getValue()如何知道返回的是Date类型呢

疑问二:为什么下面一段代码不能辣样重写equals的方法?

class Pair<T> {private T value;public T getValue() {return value;}public void setValue(T value) {this.value = value;}
//    @Override
//    public boolean equals(T t){
//    	return null;
//    }@Overridepublic boolean equals(Object obj) {// TODO Auto-generated method stubreturn super.equals(obj);}
}

疑问三:为什么写Object时,printCollection()方法会爆错

 class GernericTest {  public static void main(String[] args) throws Exception{  List<Integer> listInteger =new ArrayList<Integer>();  List<String> listString =new ArrayList<String>();printCollection(listInteger);printCollection(listString);}public static void printCollection(Collection<Object> collection){for(Object obj:collection){System.out.println(obj);}}
}


  相关解决方案