*import java.util.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.*;
public class LocaleProgram {
public static void main(String[] args) {
try {
Class c = Class.forName("java.util.Locale");
Class c1 = Class.forName("java.util.Locale$Category");
Class[] paramTypes = { c1 };
try {
Method m = c.getMethod("getDefault", paramTypes);
try {
//Object o = m.invoke(c1);
Object o = m.invoke(c1,new Object[]{"FORMAT"});
System.out.println("Object:" + (Locale)o);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}*
基本上我希望 Locale.getDefault(Locale.Category.FORMAT) 使用反射,这样我就可以在 Java 7 以外的版本上运行此代码。但是我在调用调用方法时遇到了麻烦,它给了我 java.lang.IllegalArgumentException: argument type不匹配异常。
提前谢谢了