我有一个.NET写的东西要转为java的,在泛型这边遇到不少麻烦。麻烦懂java的人告诉下
像C#语法中,接口可以定义返回泛型类型,在java中要怎么实现,关键他也不是泛型的接口。
public interface IActionUtils
{
bool Execute(string sqlStr);
int[] Execute(string[] sqlArr);
DataTable Query(string sqlStr);
DataTable Query(string table, string fields, string condition, Pager pager);
DataTable QueryPage(string table, string fields, string condition, Pager pager, ref int totalRecords);
T QueryEntity<T>(string sqlStr) where T : class, new();
IList<T> QueryEntities<T>(string sqlStr) where T : class, new();
IList<T> QueryEntities<T>(string table, string fields, string condition, Pager pager) where T : class, new();
bool Add(object obj);
bool Update(object obj);
IList<T> QueryEntities<T>(string table, string fields, string condition, Pager pager, ref int totalRecords) where T : class, new();
}
------解决方案--------------------
public <T extends Object> T get(){
return (T)new StringBuffer("test");
}
public <T> List<T> getList(){
return new ArrayList<T>();
}
------解决方案--------------------
为了兼容低版本jdk,java的泛型在编译后会被擦除,我们无法使用T.class的方式动态的获取一个泛型对象Class。
建议转换为
Object QueryBean(String classPath){
Object obj = null;
try {
Class clazz = Class.forName(classPath);
obj = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();