我要写个方法,传一个能代表任意类型的参数,不能是Object,因为我那个参数必须是个不确定的数据类型。
比如
public static String type(String x, type j){
//x是接受的字符串
for(; ;){
try{
j x=(j)x;
return x;
break;
}catch(Exception y){
system.out.print("你输入的信息和类型不匹配,请重新输入:");
continue;
}
}
}
------解决方案--------------------
楼主的问题描述的不清楚啊
------解决方案--------------------
- Java code
/** * @param <T> * @param dataList 对象集合 * @param T 对象 * @param tAttrs 对象属性集合 */public <T> void saveList(List<T> dataList, Class<T> T, List<String> tAttrs) { try { if (dataList != null && dataList.size() >0) { for (int i = 0; i < dataList.size(); i++) { if (tAttrs != null && tAttrs.size() != 0) { for (String attr : tAttrs) { String methodName = "get" + attr.substring(0, 1).toUpperCase() + attr.substring(1); Method method = T.getMethod(methodName); Object obj = method.invoke(dataList.get(i)); System.out.println(obj); } } } } } catch (Exception e) { e.printStackTrace(); }}
------解决方案--------------------
我觉得楼主需要的可能是 这个 Joda-Convert provides a small set of classes to aid conversion between Objects and Strings. It is not intended to tackle the wider problem of Object to Object transformation.