淺談java中定義泛型類(lèi)和定義泛型方法的寫(xiě)法
更新時(shí)間:2017年02月22日 10:45:01 投稿:jingxian
下面小編就為大家?guī)?lái)一篇淺談java中定義泛型類(lèi)和定義泛型方法的寫(xiě)法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
1、方法中的泛型
public static <T> T backSerializable(Class<T> clazz , String path ,String fileName){
FileInputStream fis = null;
ObjectInputStream ois = null;
Object obj = null;
try {
fis = new FileInputStream(path + fileName);
ois = new ObjectInputStream(fis);
obj = ois.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if( fis!=null) fis.close();
if( ois!=null) ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return (T)obj;
}
2、定義泛型類(lèi)
public class PageHibernateCallback<T> implements HibernateCallback<List<T>>{
private String hql;
private Object[] params;
private int startIndex;
private int pageSize;
public PageHibernateCallback(String hql, Object[] params,
int startIndex, int pageSize) {
super();
this.hql = hql;
this.params = params;
this.startIndex = startIndex;
this.pageSize = pageSize;
}
public List<T> doInHibernate(Session session) throws HibernateException,
SQLException {
//1 執(zhí)行hql語(yǔ)句
Query query = session.createQuery(hql);
//2 實(shí)際參數(shù)
if(params != null){
for(int i = 0 ; i < params.length ; i ++){
query.setParameter(i, params[i]);
}
}
//3 分頁(yè)
query.setFirstResult(startIndex);
query.setMaxResults(pageSize);
return query.list();
}
}
以上這篇淺談java中定義泛型類(lèi)和定義泛型方法的寫(xiě)法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot 自動(dòng)配置的實(shí)現(xiàn)
這篇文章主要介紹了Spring Boot 自動(dòng)配置的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Java FTPClient實(shí)現(xiàn)文件上傳下載
這篇文章主要為大家詳細(xì)介紹了Java FTPClient實(shí)現(xiàn)文件上傳下載的相關(guān)資料,需要的朋友可以參考下2016-04-04
?Java數(shù)據(jù)結(jié)構(gòu)的十大排序
這篇文章主要介紹了?Java數(shù)據(jù)結(jié)構(gòu)的十大排序,排序算法分為比較類(lèi)排序和非比較類(lèi)排序,具體的內(nèi)容,需要的朋友參考下面思維導(dǎo)圖及文章介紹,希望對(duì)你有所幫助2022-01-01
java生成字母數(shù)字組合的隨機(jī)數(shù)示例 java生成隨機(jī)數(shù)
這篇文章主要介紹了java生成字母數(shù)字組合的隨機(jī)數(shù)的示例,大家參考使用吧2014-01-01
java 使用DecimalFormat進(jìn)行數(shù)字的格式化實(shí)例詳解
這篇文章主要介紹了java 使用DecimalFormat進(jìn)行數(shù)字的格式化實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06

