Person类
public class Person {
//私有属性
private Long id;
//公有属性
public String name;
//无参共有构造方法
public Person() {
super();
this.name = "lyf";
this.id = 1L;
}
//有参共有构造方法
public Person(Long id, String name) {
super();
this.id = id;
this.name = name;
}
public Person(int b,String...a) {
System.out.print(b+" ");
for(int i=0;i<a.length;i++) {
System.out.println(a[i]);
}
}
public Person(Long id) {
super();
this.id = id;
}
//有参私有构造方法
private Person(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
//静态有参方法
public static void run(int num) {
System.out.println("run:"+num);
}
//私有成员函数
private String getSomeThing(int num) {
return "aaaaaa:"+num;
}
//共有成员函数
@Override
public String toString() {
return "Person [name=" + name + ", id=" + id + "]";
}
}
测试类
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
public class MyReflect {
public String className = null;
public Class personClass = null;
@Before
public void init() throws ClassNotFoundException {
className="luoyunfan.reflect.Person";
personClass = Class.forName(className);
//2 要提前知道有这个类。
// Class a = Person.class;
//3 没用,需要new个对象。
// Person person = new Person() ;
// personClass = person.getClass();
}
/**
* 打印类名
*/
@Test
public void getClassName() {
System.out.println(personClass.getName());
}
@Test
public void getClassName2()
{
System.out.println(Person.class);
}
//通过类创建实例化对象(实际上是调用无参构造)
@Test
public void getNewInstance() throws InstantiationException, IllegalAccessException {
System.out.println(personClass.newInstance());
Person person = (Person) personClass.newInstance();
System.out.println(person.getId());
System.out.println(person.getName());
}
//调用无参构造,通过无参实例化对象。
@Test
public void getWucanConstructor() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor constructor = personClass.getConstructor();
Person person = (Person)constructor.newInstance();
System.out.println(person.getId());
System.out.println(person.getName());
}
//通过公有构造函数实例化对象。
@Test
public void getPublicConstructor() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor constructor = personClass.getConstructor(Long.class);
Person person = (Person)constructor.newInstance(100L);
System.out.println(person.getId());
System.out.println(person.getName());
}
//通过私有构造函数实例化对象。
@Test
public void getPrivateConstructor() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor constructor = personClass.getDeclaredConstructor(String.class);
//设置方法可见
//“Declared”也可获取公有的函数
constructor.setAccessible(true);
Person person = (Person)constructor.newInstance("aaa");
System.out.println(person.getId());
System.out.println(person.getName());
}
//可变参数构造函数实例化对象
@Test
public void getChange() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor constructor = personClass.getConstructor(int.class,String[].class);
Object obj = constructor.newInstance(3,new String[]{"aaa","bbb"});
}
//多参数构造方法实例化对象//通过Filed来改变实例化公有对象的属性。
@Test
public void getNotPrivateField() throws Exception {
//实际上也是去调用Long,String的字节码文件。
Constructor constructor = personClass.getConstructor(Long.class,String.class);
Object obj = constructor.newInstance(100L,"aaa");
Field field = personClass.getField("name");
field.set(obj, "lisi");
System.out.println(field.get(obj)+","+obj);
}
//多参数构造方法实例化对象//通过Filed来改变实例化私有对象的属性。
@Test
public void getPrivateField() throws Exception{
Constructor constructor = personClass.getConstructor(Long.class,String.class);
Object obj = constructor.newInstance(100L,"aaa");
Field filed = personClass.getDeclaredField("id");
filed.setAccessible(true);
filed.set(obj, 200L);
System.out.println(filed.get(obj));
}
//执行非静态公有函数
@Test
public void getNotPrivateMethod() throws Exception{
System.out.println(personClass.getMethod("toString"));
Object obj = personClass.newInstance();
Object object = personClass.getMethod("toString").invoke(obj);
System.out.println(object);
}
//执行非静态私有函数
@Test
public void getPrivateMethod() throws Exception{
Object obj = personClass.newInstance();
Method method = personClass.getDeclaredMethod("getSomeThing",int.class);
method.setAccessible(true);
Object obj1 = method.invoke(obj,6);
System.out.println(obj1);
}
//执行静态公有函数
@Test
public void getStaticMethod() throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
Method me = personClass.getDeclaredMethod("run",int.class);
//默认是返回值,如果是void方法,则返回null
Object obj = me.invoke(null, 6);
System.out.println(obj);
}
//其他
@Test
public void otherMethod() throws Exception{
}
}