1.反射是什么
反射允许对成员变量,成员方法,构造方法的信息进行编程访问
2.获取class对象的三种方式
(1)class.forName(“全类名”);
(2)类名.class
(3)对象.getClass();
public class MyReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException {
Class clazz1 = Class.forName("com.klx.myreflect.Student");
Class clazz2 = Student.class;
Student s = new Student();
Class clazz3 = s.getClass();
System.out.println(clazz1 == clazz2);
System.out.println(clazz2 == clazz3);
}
}
3.获取构造方法
public class MyReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
/*
Class类中用于获取构造方法的方法
Constructor<?>[] getConstructors()
Constructor<?>[] getDeclaredConstructors()
Constructor<?> getConstructor(Class<?>... parameterTypes)
Constructor<?> getDeclaredConstructor(Class<?>... parameterTypes)
Constructor类中用于创建对象的方法
T newInstance(Object... initargs)
setAccessible(boolean flag)
*/
//1.获取class字节码文件对象
Class clazz = Class.forName("com.klx.myreflect2.Student");
//2.获取构造方法
/* Constructor[] cons = clazz.getConstructors();
for (Constructor con : cons) {
System.out.println(con);
}*/
/*Constructor[] cons2 = clazz.getDeclaredConstructors();
for (Constructor con : cons2) {
System.out.println(con);
}
*/
Constructor con1 = clazz.getDeclaredConstructor();
System.out.println(con1);
Constructor con2 = clazz.getDeclaredConstructor(String.class);
System.out.println(con2);
Constructor con3 = clazz.getDeclaredConstructor(int.class);
System.out.println(con3);
Constructor con4 = clazz.getDeclaredConstructor(String.class, int.class);
System.out.println(con4);
//获取构造方法的权限修饰符
/*int modefiers = con4.getModifiers();
System.out.println(modefiers);
Parameter[] parameters = con4.getParameters();
for (Parameter parameter : parameters) {
System.out.println(parameter);
}*/
//临时取消权限校验
con4.setAccessible(true);
Student stu = (Student) con4.newInstance("zhangsan", 24);
System.out.println(stu);
}
}
4.获取成员变量
public class MyReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Class clazz = Class.forName("com.klx.myreflect3.Student");
//获取成员变量
Field[] fields = clazz.getFields();
for (Field field : fields) {
System.out.println(field);
}
//获取单个的成员变量
Field name = clazz.getDeclaredField("name");
System.out.println(name);
//获取权限修饰符
int modifiers = name.getModifiers();
System.out.println(modifiers);
//获取成员变量的名字
String name1 = name.getName();
System.out.println(name1);
//获取成员变量的数据类型
Class type = name.getType();
System.out.println(type);
//获取成员变量记录的值
Student s = new Student("zhangsan", 23,"男");
name.setAccessible(true);
String value = (String) name.get(s);
System.out.println(value);
//修改对象里面记录的值
name.set(s, "lisi");
System.out.println(s);
}
}
5.获取成员方法
public class MyReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
/*
Class类中用于获取成员方法的方法
Method[] getMethods():返回所有公共成员方法对象的数组,包括继承的
Method[] getDeclaredMethods():返回所有成员方法对象的数组,不包括继承的
Method getMethod(String name, Class<?>... parameterTypes) :返回单个公共成员方法对象
Method getDeclaredMethod(String name, Class<?>... parameterTypes):返回单个成员方法对象
Method类中用于创建对象的方法
Object invoke(Object obj, Object... args):运行方法
参数一:用obj对象调用该方法
参数二:调用方法的传递的参数(如果没有就不写)
返回值:方法的返回值(如果没有就不写)
获取方法的修饰符
获取方法的名字
获取方法的形参
获取方法的返回值
获取方法的抛出的异常
*/
//1. 获取class字节码文件对象
Class clazz = Class.forName("com.klx.myreflect4.Student");
//2. 获取里面所有的方法对象(包含父类中所有的公共方法)
/* Method[] methods = clazz.getMethods();
for (Method method : methods) {
System.out.println(method);
}*/
// 获取里面所有的方法对象(不能获取父类的,但是可以获取本类中私有的方法)
/*Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
System.out.println(method);
}*/
// 获取指定的单一方法
Method m = clazz.getDeclaredMethod("eat", String.class);
System.out.println(m);
// 获取方法的修饰符
int modifiers = m.getModifiers();
System.out.println(modifiers);
// 获取方法的名字
String name = m.getName();
System.out.println(name);
// 获取方法的形参
Parameter[] parameters = m.getParameters();
for (Parameter parameter : parameters) {
System.out.println(parameter);
}
//获取方法的抛出的异常
Class[] exceptionTypes = m.getExceptionTypes();
for (Class exceptionType : exceptionTypes) {
System.out.println(exceptionType);
}
//方法运行
/*Method类中用于创建对象的方法
Object invoke(Object obj, Object... args):运行方法
参数一:用obj对象调用该方法
参数二:调用方法的传递的参数(如果没有就不写)
返回值:方法的返回值(如果没有就不写)*/
Student s = new Student();
m.setAccessible(true);
//参数一s:表示方法的调用者
//参数二"汉堡包":表示在调用方法的时候传递的实际参数
String result = (String) m.invoke(s, "汉堡包");
System.out.println(result);
}
}