package com.test.reflect;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* AccessibleObject 类是 Field、Method 和 Constructor 对象的基类。
* Constructor->Method-Field->invoke
*/
public class Reflect01 {
public static void main(String[] args) {
createReflect();
create01Class();//类
create02Method();//方法
create03Field();//字段
}
//获取实例
public static void create01Class() {
Class clazz;
try {
clazz = Class.forName("com.test.reflect.Person");//获取字节码对象
//1.无参
Object p1 = clazz.newInstance();
Person p2 = (Person)clazz.getConstructor(String.class).newInstance();
//2.有参
Person p3 = (Person)clazz.getConstructor(String.class).newInstance("lisi");
System.out.println(p3.getName());
// 创建了person!lisi
// lisi
}catch(Exception e){
e.printStackTrace();
}
}
private static void createReflect() {
//1.通过实例
//Class类是描述类的类
Class clazz = Person.class;
System.out.println(clazz);
//class com.test.reflect.Person
//创建了person! //说明调用了Person类的空参构造函数
//2.通过getClass
Person p = new Person();
Class clazz1 = p.getClass();
System.out.println(clazz1);
//创建了person! //说明调用了Person类的空参构造函数
//class com.test.reflect.Person
//3.Class.forName(推荐)
try {
Class clazz2 = Class.forName("com.test.reflect.Person");
System.out.println(clazz2);
//创建了person! //说明调用了Person类的空参构造函数
//class com.test.reflect.Person
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//三种一块执行,只创建一次实例,因为是运行时
//4.获取字节码文件的其他方式
com.test.reflect.Person p2 = new com.test.reflect.Person();
}
private static void create03Field() {
Class clazz;
try {
clazz = Class.forName("com.test.reflect.Person");//获取字节码对象
Person person = (Person)clazz.newInstance();//实例化当前字节码对象
Field field = clazz.getField("sex");
person.setSex("12");//注意public还是其他的
System.out.println(field.get(person));
// 创建了person!
// 12
} catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
}
private static void create02Method() {
try {
Class clazz = Class.forName("com.test.reflect.Person");//获取字节码对象
Object person = clazz.newInstance();//实例化当前字节码对象
//1.调用空参
Method method = clazz.getMethod("run", null);
method.invoke(person, null);
System.out.println(method);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
package com.test.reflect;
public class Person {
private String name;
public String sex;
private int age;
private String health;
public Person(){
System.out.println("创建了person!");
}
public Person(String name){
this.name = name;
System.out.println("创建了person!" + name);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getHealth() {
return health;
}
public void setHealth(String health) {
this.health = health;
}
public void run(){
System.out.println("run------->");
}
public void show(String name,int age){
System.out.println("show------->");
}
}