浅谈java中定义泛型类和定义泛型方法的写法
程序员文章站
2024-03-06 21:47:50
1、方法中的泛型
public static t backserializable(class clazz , string...
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、定义泛型类
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 执行hql语句 query query = session.createquery(hql); //2 实际参数 if(params != null){ for(int i = 0 ; i < params.length ; i ++){ query.setparameter(i, params[i]); } } //3 分页 query.setfirstresult(startindex); query.setmaxresults(pagesize); return query.list(); } }
以上这篇浅谈java中定义泛型类和定义泛型方法的写法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。