本帖最后由 贾飞雨 于 2012-8-12 22:29 编辑
public class ReflectTest {
/**
* @param args
* @throws ClassNotFoundException
* @throws NoSuchMethodException
* @throws SecurityException
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
String str1 = "aba";
Class cls1 = str1.getClass();
Class cls2 = String.class;
Class cls3 = Class.forName("java.lang.String");
System.out.println(cls1==cls2);
System.out.println(cls2==cls3);
System.out.println(cls1.isPrimitive());
System.out.println(int.class.isPrimitive());
System.out.println(int.class == Integer.class);
System.out.println(int.class == Integer.TYPE);
System.out.println(int[].class.isPrimitive());
System.out.println(int[].class.isPrimitive());
//new String(new StringBuffer("abc"));
Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
String str2 = (String)constructor1.newInstance(StringBuffer.class);
System.out.println(str2.charAt(1));
ReflctPoint pt1 = new ReflctPoint(3, 5);
Field fieldY = pt1.getClass().getField("Y");
System.out.println(fieldY.get(pt1));
//getChangeString(pt1);
System.out.println(pt1);
}
}
错误显示的是32行,也就是红色字那里,下面是错误提示,大家帮我看一下,明明和张老师的一样啊,
true
true
false
true
false
true
false
false
Exception in thread "main" java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at shiyi.ReflectTest.main(ReflectTest.java:32)
|
|