Java中动态变量类型的解决方案
处理这种情况的最佳方法是什么。根据调用Helper方法的控制器方法,将为Helper方法构造函数提供各种类型。因此getMethodOne调用helper方法并提供typeone类型参数。但是,助手方法的列表泛型类型给出了未知的类错误Java中动态变量类型的解决方案,java,spring,list,generics,Java,Spring,List,Generics,处理这种情况的最佳方法是什么。根据调用Helper方法的控制器方法,将为Helper方法构造函数提供各种类型。因此getMethodOne调用helper方法并提供typeone类型参数。但是,助手方法的列表泛型类型给出了未知的类错误 @RequestMapping(value = "/methodOne", method = RequestMethod.GET) public List<?> getMethodOne(){ return Helper(typeone);
@RequestMapping(value = "/methodOne", method = RequestMethod.GET)
public List<?> getMethodOne(){
return Helper(typeone);
}
@RequestMapping(value = "/methodTwo", method = RequestMethod.GET)
public List<?> getMethodTwo(){
return Helper(typetwo);
}
private List<?> Helper(type){
// type in List<type> - gives unknown class error
new List<type> someObject = new ArrayList<type>();
return someObject;
}
@RequestMapping(value=“/methodOne”,method=RequestMethod.GET)
公共列表getMethodOne(){
返回助手(第一类);
}
@RequestMapping(value=“/methodTwo”,method=RequestMethod.GET)
公共列表getMethodTwo(){
返回助手(第二类);
}
私人列表帮助器(类型){
//在列表中键入-给出未知类错误
new List someObject=new ArrayList();
返回某个对象;
}
我尝试了一个由typeone和typetwo对象实现的IType接口,然后在helper方法中更新了参数,如下所示
private List<Itype> Helper(IType type){
new List<type> someObject = new ArrayList<type>();
return someObject;
}
私有列表帮助器(IType类型){
new List someObject=new ArrayList();
返回某个对象;
}
它应该更像
private <T> List<T> Helper(Clazz<T> clazz){ // if you need that at all here
List<T> someObject = new ArrayList<T>();
return someObject;
}
private-List-Helper(Clazz-Clazz){//如果您在这里需要它的话
List someObject=new ArrayList();
返回某个对象;
}
您的泛型构造函数语法错误。