返回动态类型我如何写一个方法(如果有的话,我可以),将返回一个动态类型方法在Java
像
public X createRequestObject(Class xclass , String url , String username , String password){
X x = Class.forName(xclass.getCannonicalName()).getConstructor(String.class).newInstance(url);
x.setheader("AUTHORIZATION" , createHeader(username,password)
return x
}
,然后我就可以使用它像
HttpGet httpGet = createRequestObject(HttpGet.class , "http://wwww.google.com , "username","password");
or
HttpPost httpPost = createRequestObject(HttpPost.class , "http://wwww.google.com , "username","password");
我知道我可以返回一个对象,然后再投,但我不喜欢这样蒙上想知道是否有Java中的结构,可以帮助我做到这一点
你到底在做什么? –
这篇文章应该有答案:http://stackoverflow.com/questions/450807/java-generics-how-do-i-make-the-method-return-type-generic?rq=1 – wanghq
可能的重复问题:http://stackoverflow.com/questions/2669326/java-generic-return-type有更多的提示和建议那里... – jwest