下面小编就为大家带来一篇浅谈java中定义泛型类和定义泛型方法的写法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
1、方法中的泛型
public static T backSerializable(Class 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 implements HibernateCallback>{ 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 doInHibernate(Session session) throws HibernateException, sqlException { //1 执行hql语句 Query query = session.createquery(hql); //2 实际参数 if(params != null){ for(int i = 0 ; i
以上这篇浅谈java中定义泛型类和定义泛型方法的写法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。