package demo.api;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class Person {
public String name;
private int age;
public Person() {
System.out.println("Person空参构造方法执行");
}
Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Person含参构造方法执行");
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
int getAge() {
return age;
}
void setAge(int age) {
this.age = age;
}
void show() {
System.out.println("我是" + name + ",今年" + age + "岁");
}
@Override
public String toString() {
return "名字:" + getName() + ",年龄:" + getAge();
}
}
class Class类 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException, IOException {
}
private static void getMethod(String className) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class clazz = Class.forName(className);
Method[] methods = clazz.getDeclaredMethods();
System.out.println(className + "类的所有方法:");
for (Method method : methods) {
System.out.println("\t" + method);
}
Method[] methods2 = clazz.getMethods();
System.out.println(className + "类的共有方法(包含父类方法):");
for (Method method : methods2) {
System.out.println("\t" + method);
}
Method method = clazz.getDeclaredMethod("show", null);
Constructor constructor = clazz.getDeclaredConstructor(String.class, int.class);
Object o = constructor.newInstance("阿基米德", 38);
method.invoke(o, null);
Method method2 = clazz.getDeclaredMethod("setName", String.class);
method2.invoke(o, "米开朗基罗");
method.invoke(o, null);
}
private static void getField(String className) throws ClassNotFoundException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, InstantiationException {
Class clazz = Class.forName(className);
Field fieldName = clazz.getField("name");
System.out.println("字段信息:" + fieldName);
Field fieldAge = clazz.getDeclaredField("age");
System.out.println("Declared字段信息:" + fieldAge);
Field[] fields = clazz.getDeclaredFields();
System.out.println(className + "类的所有字段:");
for (Field field : fields) {
System.out.println("\t" + field);
}
Constructor constructor = clazz.getConstructor();
Object o = constructor.newInstance();
fieldAge.setAccessible(true);
fieldAge.set(o, 18);
System.out.println("打印设置后的Person对象:" + o);
Object age = fieldAge.get(o);
System.out.println("指定Person的age内容: " + age);
}
private static void createClassObject(String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class clazz = Class.forName(className);
Constructor constructor = clazz.getConstructor();
Object o = constructor.newInstance();
Constructor constructor2 = clazz.getDeclaredConstructor(String.class, int.class);
Object o2 = constructor2.newInstance("小明", 18);
}
private static void getClassObject() throws ClassNotFoundException {
Person p = new Person();
Class clazz = p.getClass();
Class clazz2 = Person.class;
System.out.println(clazz == clazz2);
Class clazz3 = Class.forName("demo.api.Person");
System.out.println(clazz3);
}
}