1、package enums;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.List;import org.junit.Test;public class Refance2 Testpublic void test1() throws ExceptionClass clazz=Class.forName(“enums.Person“);Constructor c=clazz.get
2、Constructor(null);Person p=(Person) c.newInstance(null);System.out.println(p.name);Testpublic void test2() throws ExceptionClass clazz=Class.forName(“enums.Person“);Constructor c=clazz.getConstructor(String.class);Person p=(Person) c.newInstance(“王亚楠“);System.out.println(p.name);Testpublic void test
3、3() throws ExceptionClass clazz=Class.forName(“enums.Person“);Constructor v=clazz.getConstructor(String.class,int.class);Person p=(Person) v.newInstance(“王亚楠“,123);System.out.println(p.name);Testpublic void test4() throws ExceptionClass clazz=Class.forName(“enums.Person“);Constructor c=clazz.getDecl
4、aredConstructor(List.class);c.setAccessible(true);/暴力反射Person p=(Person) c.newInstance(new ArrayList();System.out.println(p.name+“4“);Test/创建对象的另外一种途径public void test5() throws ExceptionClass clazz=Class.forName(“enums.Person“);Person p=(Person) clazz.newInstance();System.out.println(p.name);class Personpublic String name=“王亚楠 “;public Person() System.out.println(“person“);public Person(String name)System.out.println(“name“+name);public Person(String name,int password)System.out.println(name+“:“+password);private Person(List list)System.out.println(“list“);