论坛首页 Java企业应用论坛

提问:Java5泛型的T.class的获取

浏览 20433 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-04-07  
我来补充下ecsoftcn说的需要带参数得方法
public class BaseHelper<T> {
	private T object = null;
	private HttpServletRequest request;

	public BaseHelper(HttpServletRequest request, String key, Class<T> clazz, String[] args, Class<String>... argTypes) {
		this.request = request;
		
		object = (T)request.getSession().getAttribute(key);
		if(object == null){
		  Constructor constructor ;
		  try{
			constructor = clazz.getConstructor(argTypes);
			object = constructor.newInstance(args);
		  }
		  catch(NoSuchMethodException e){}
		  catch(IllegalAccessException e){}		
		  catch(InvocationTargetException e){}
		  catch(InstantiationException e){}
		}
	}
	
	public T get(){
		return object;
	}
}

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics