package com.njust.zht.Reflection; public class Dog { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } private String color; private int age; public void wangwang(){ System.out.println("汪汪叫。。"); System.out.println("dog name="+name); System.out.println("dog color="+color); System.out.println("dog age="+age); } }
package com.njust.zht.Test1; import com.njust.zht.Reflection.Dog; public class Test1 { public static void main(String[] args) throws ClassNotFoundException { Class<?> demo1=null; // Class<?> demo2=null; // Class<?> demo3=null; demo1=Class.forName("com.njust.zht.Reflection.Dog"); Dog dog=null; try { dog=(Dog) demo1.newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } dog.setName("lili"); dog.setColor("black"); dog.setAge(2); dog.wangwang(); } }
汪汪叫。。 dog name=lili dog color=black dog age=2
public Dog(){
}
java.lang.InstantiationException: com.njust.zht.Reflection.Dog at java.lang.Class.newInstance(Unknown Source) at com.njust.zht.Test1.Test1.main(Test1.java:16) Exception in thread "main" java.lang.NullPointerException at com.njust.zht.Test1.Test1.main(Test1.java:24)
package com.njust.zht.Test1; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import com.njust.zht.Reflection.Dog; public class Test1 { public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, InvocationTargetException { Class<?> demo=null; // Class<?> demo2=null; // Class<?> demo3=null; demo=Class.forName("com.njust.zht.Reflection.Dog"); Dog dog1=null; Dog dog2=null; Dog dog3=null; Dog dog4=null; try { //获取所有的构造函数 Constructor<?> cons[]=demo.getConstructors(); System.out.println(cons[0]); System.out.println(cons[1]); System.out.println(cons[2]); System.out.println(cons[3]); dog4=(Dog) cons[0].newInstance("niuniu",15,"red");/************************************/ dog3=(Dog) cons[1].newInstance("lolo",20); dog2=(Dog) cons[2].newInstance("kity"); dog1=(Dog) cons[3].newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(dog1); System.out.println(dog2); System.out.println(dog3); System.out.println(dog4); } }上面的标记
/************************************/出如果参数设置不对应就会出错。可以先输出下构造函数里面的参数解决。在我这里是按照参数从多到少的顺序配置的。(本人在此处折腾了几个小时,最后想到输出构造函数的方法解决之)如果设置错误就会有如下的错误:
Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at javaGXJS.fanShe.reflect.main(dfgfdgfdg.java:45)