package reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionAPIDemo {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
// 获取Employee这个类所关联的Class对象
Class<?> classType = Class.forName("reflection.Employee2");
// 通过反射机制来构造Employee这个类的实例对象(默认调用无参构造)
//Employee2 employee2 = (Employee2) classType.newInstance();
// 调用指定的方法来构造对象(得到无参构造方法)
// Constructor<?> constructor = classType.getConstructor(new Class[]{});
// Employee2 employee2 = (Employee2)constructor.newInstance(new Object[]{});
// System.out.println(employee2);
// 调用指定的方法来构造对象(得到有参构造方法)
Constructor<?> constructor = classType.getConstructor(new Class[]{String.class,int.class});
Employee2 employee2 = (Employee2)constructor.newInstance(new Object[]{"小王",30});
System.out.println(employee2);
//获得Class对象指定的方法,包括私有的方法
Method method = classType.getDeclaredMethod("toString",new Class[]{});
System.out.println(method.getName());
//方法的调用
String desc = (String)method.invoke(employee2,new Object[]{});
System.out.println(desc);
// 获取Class对象所有的方法
Method[] methods = classType.getDeclaredMethods();
System.out.println(methods.length);
for (Method m : methods ) {
// 打印方法名+方法的访问权限+方法的返回值类型
System.out.println(m.getName()+"---"+m.getModifiers()+"---"+m.getReturnType());
}
// 改变私有方法的访问权限
Method method1 = classType.getDeclaredMethod("work",new Class[]{});
method1.setAccessible(true); //true代表是可以访问的
method1.invoke(employee2,new Object[]{});
// 获取Class对象指定的属性,包括私有的
Field field = classType.getDeclaredField("name");
field.setAccessible(true);
field.set(employee2,"鲁班七号");
System.out.println(field.get(employee2));
}
}
class Employee2{
private String name;
private int age;
public Employee2() {
}
public Employee2(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee2{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
private void work(){
System.out.println("看看能不能访问私有的方法");
}
}
package reflection;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionAPIDemo {
public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
// 获取Employee这个类所关联的Class对象
Class<?> classType = Class.forName("reflection.Employee2");
// 通过反射机制来构造Employee这个类的实例对象(默认调用无参构造)
//Employee2 employee2 = (Employee2) classType.newInstance();
// 调用指定的方法来构造对象(得到无参构造方法)
// Constructor<?> constructor = classType.getConstructor(new Class[]{});
// Employee2 employee2 = (Employee2)constructor.newInstance(new Object[]{});
// System.out.println(employee2);
// 调用指定的方法来构造对象(得到有参构造方法)
Constructor<?> constructor = classType.getConstructor(new Class[]{String.class,int.class});
Employee2 employee2 = (Employee2)constructor.newInstance(new Object[]{"小王",30});
System.out.println(employee2);
//获得Class对象指定的方法,包括私有的方法
Method method = classType.getDeclaredMethod("toString",new Class[]{});
System.out.println(method.getName());
//方法的调用
String desc = (String)method.invoke(employee2,new Object[]{});
System.out.println(desc);
// 获取Class对象所有的方法
Method[] methods = classType.getDeclaredMethods();
System.out.println(methods.length);
for (Method m : methods ) {
// 打印方法名+方法的访问权限+方法的返回值类型
System.out.println(m.getName()+"---"+m.getModifiers()+"---"+m.getReturnType());
}
// 改变私有方法的访问权限
Method method1 = classType.getDeclaredMethod("work",new Class[]{});
method1.setAccessible(true); //true代表是可以访问的
method1.invoke(employee2,new Object[]{});
// 获取Class对象指定的属性,包括私有的
Field field = classType.getDeclaredField("name");
field.setAccessible(true);
field.set(employee2,"鲁班七号");
System.out.println(field.get(employee2));
}
}
class Employee2{
private String name;
private int age;
public Employee2() {
}
public Employee2(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Employee2{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
private void work(){
System.out.println("看看能不能访问私有的方法");
}
}