什么是反射?
反射允许对封装类的字段,方法和构造函数的信息进行编程访问。
获取class对象的三种方式
3种方式的使用场景
全类名获取方法
用反射获取构造方法
java
package com.test12;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Test {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
//获取字节码文件对象clazz
Class clazz = Class.forName("com.test12.Student");
//获取public修饰的构造方法;不带Declared只能获取公共的
/* Constructor[] cons = clazz.getConstructors();
for (Constructor con : cons) {
System.out.println(con);
}*/
//获取所有的构造方法
/* Constructor[] cons = clazz.getDeclaredConstructors();
for (Constructor con : cons) {
System.out.println(con);
}*/
//获取单个构造方法
//空参类型
/*Constructor con = clazz.getDeclaredConstructor();
System.out.println(con);*/
//String类型
/*Constructor con = clazz.getDeclaredConstructor(String.class);
System.out.println(con);*/
//int类型
/*Constructor con = clazz.getDeclaredConstructor(int.class);
System.out.println(con);*/
//多参类型
Constructor con = clazz.getDeclaredConstructor(String.class,int.class);
//获取构造方法的权限修饰符
int modifiers = con.getModifiers();
//获取构造方法的参数
/*Parameter[] parameters = con.getParameters();
for (Parameter parameter : parameters) {
System.out.println(parameter);
}*/
//暴力反射:临时取消权限校验
con.setAccessible(true);
//根据获取的构造方法创建对象
Student stu = (Student) con.newInstance("zhangsan", 23);
System.out.println(stu);
}
}
用反射获取成员变量
用反射获取成员方法
java
package com.test12;
import java.io.IOException;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name) {
this.name = name;
}
protected Student(int age) {
this.age = age;
}
private Student(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;
}
private String eat(String name) throws IOException,NullPointerException,ClassCastException {
System.out.println(name + "在吃东西");
return "hahaha";
}
private void eat(String name, int a){
System.out.println(name + "在吃东西");
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
java
package com.test12;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
public class Test2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class clazz = Class.forName("com.test12.Student");
//获取所有的公共方法对象(包括父类里的方法对象)
/*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("toString");
System.out.println(m);*/
//单参方法
Method m = clazz.getDeclaredMethod("eat", String.class);
System.out.println(m);
//多参方法
/*Method m = clazz.getDeclaredMethod("eat", String.class,int.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);
}
//方法运行
Student s = new Student();
//无返回值类型方法
/*m.setAccessible(true);
m.invoke(s,"zhangsan");*/
//有返回值类型方法
m.setAccessible(true);
String result = (String) m.invoke(s,"zhangsan");
System.out.println(result);
}
}