Java反射机制
反射机制原理图如下:
以下是例子:
package com.jinzhi.test1;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Demo3 {
public static void main(String[] args) {
//利用反向加载的机制来通过类名自动调用它的方法,和属性
//得到JVM中class文件的类Class:3种方法
Class c1 = Student.class;
Class c2 = new Student().getClass();
try {
Class c3 = Class.forName("com.jinzhi.test1.Student");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//调用无参构造方法-Constructor
Student s1 = null;
try {
s1 = (Student)c1.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
s1.setName("周星驰");
s1.setAge(24);
s1.setSex("男");
System.out.println(s1.getName() + " / " + s1.getSex() + " / " + s1.getAge());
System.out.println("------------------------------------------");
//调用类的无参构造
//得到构造方法
try {
Constructor c = c1.getConstructor(String.class, int.class, String.class);
Student s2 = (Student)c.newInstance("路飞", 24, "男");
System.out.println(s2.getName()+"/" + s2.getAge() +"/"+ s2.getSex());
System.out.println("------------------------------------------");
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//得到类的属性
try {
Field f = c1.getDeclaredField("name");
Student s3 = (Student)c1.newInstance();
//还要设置私有属性可访问的属性
//f.setAccessible(true);
f.set(s3, "男神");
System.out.println("我的姓名 " + s3.getName());
System.out.println("-------------------------------");
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//得到一般方法
try {
Method m = c1.getDeclaredMethod("setName", String.class);
Student s4 = (Student)c1.newInstance();
//设置私有方法可访问的特性
m.setAccessible(true);
m.invoke(s4, "司马光");
System.out.println("姓名:" + s4.getName());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}