泛型方法指定返回值类型

public static <T> T getCache(String namespace, Class<T> clazz) throws IOException, ClassNotFoundException{

CacheItem<Serializable> item = cacheMap.get(namespace);
T t = null;
if(item == null){
File file = new File("c:/cache/" + namespace + ".txt");
if(file.exists()){
FileInputStream fileInputStream = new FileInputStream(file);
ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
t = (T) inputStream.readObject();
}
}

return t;
}

猜你喜欢

转载自zjhdreams.iteye.com/blog/2367063