我要写个方法,传一个能代表任意类型的参数,不能是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;
}
}
}
比如
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;
}
}
}
public static String type(String x, Class class)
{}
然后可以利用反射来调用方法,不能像普通的写法那么写了
具体的 LZ查一下反射的用法把
reflect
/**
* @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();
}
}