泛型方法指定返回值类型
程序员文章站
2022-04-05 14:14:25
...
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;
}
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;
}
上一篇: php怎么实现上传文件功能
下一篇: 冯保:明朝著名太监,他是个怎样的人?