反射的第一步就是先拿到class对象
一、反射获取 class对象的三种方式
- Class.forName(“类的全路径”);
- 类名.class
- 对象.getClass();
import org.junit.Test;
public class Reflection {
@Test
public void TestReflection() throws Exception {
//获取类对象第一种方式:Class.forName(“类的全路径”);
Class<?> student1 = Class.forName("src/main/java/Student.java");
//获取类对象第二种方式:类名.class
Class<Student> student2 = Student.class;
//获取类对象第三种方式:对象.getClass();
Class<?> student3 = new Student().getClass();
}
}
二、反射获取 包名类名
1、获取包名
clazz . getPackage().getName()
System.out.println(student.getPackage().getName());
2、获取类名
clazz . getSimpleName()
System.out.println(student.getSimpleName());
3、获取包名+类名
clazz . getName()
System.out.println(student.getName());
三、反射获取 构造方法
获取 公有的–public修饰的
clazz . getConstructors()
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.util.Arrays;
public class Reflection {
@Test
public void TestReflection() throws Exception {
//1、获取Class对象
Class<Student> clazz = Student.class;
//2、获取构造方法,并放入数组中
Constructor<?>[] constructors = clazz.getConstructors();
//3、遍历数组,获取每一个构造方法
/**
* for(1 2 :3)
* for(1循环得到的数据类型 2变量名 :3要循环的对象)
*/
for(Constructor c : constructors){
System.out.println(c.getName());//获取构造方法的名称
Class[] types = c.getParameterTypes();//获取构造方法的参数类型
System.out.println(Arrays.toString(types));
}
}
}
四、反射获取 成员方法
获取 公有的–public修饰的
clazz.getMethods();
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
public class Reflection {
@Test
public void TestReflection() throws Exception {
//1、获取Class对象
Class<Student> clazz = Student.class;
//2、获取成员方法,用数组接收
Method[] methods = clazz.getMethods();
//3、遍历数组,获取每个方法
for (Method m:methods
) {
System.out.println(m.getName());//获取方法名
Class<?>[] types = m.getParameterTypes();
System.out.println(Arrays.toString(types));//获取方法参数类型
}
}
}
获取 私有的–private修饰的
clazz . getDeclaredMethod();
.setAccessible(true);//先打开私有通道
import org.junit.Test;
import java.lang.reflect.Method;
public class Reflection {
@Test
public void TestReflection() throws Exception {
//1、获取Class对象
Class<Student> clazz = Student.class;
//2、获取私有方法
Method method = clazz.getDeclaredMethod("方法名save",方法参数int.class,String.class);
//3、执行私有方法
method.setAccessible(true);//先打开私有通道
//Object object = clazz.newInstance();用这个也可以
Student student = new Student();
Object o = method.invoke(student, 17, "jack");
}
}
五、反射获取 成员变量
获取 公有的–public修饰的
clazz . getFields();
import org.junit.Test;
import java.lang.reflect.Field;
public class Reflection {
@Test
public void TestReflection() throws Exception {
//1、获取Class对象
Class<Student> clazz = Student.class;
//2、获取成员变量,用数组接收
Field[] fields = clazz.getFields();
//3、获取每一个成员变量
for (Field f: fields
) {
System.out.println(f.getName());//获取变量名
System.out.println(f.getType().getName());//获取变量类型
}
}
}
获取 私有的–private修饰的
clazz . getDeclaredFields();
.setAccessible(true);//先打开私有可见
import org.junit.Test;
import java.lang.reflect.Field;
public class Reflection {
@Test
public void TestReflection() throws Exception {
//1、获取Class对象
Class<Student> clazz = Student.class;
//2、获取私有属性
Field name = clazz.getDeclaredField("name");
//3、获取属性的数据类型
System.out.println(name.getType().getName());
//4、设置私有属性的值
name.setAccessible(true);//先打开私有可见
Student student = new Student();//需要创建该对象
name.set(student,"jack");
//5、获取私有属性的值
System.out.println(name.get(student));
}
}
六、反射 创建对象
触发无参构造创建对象
clazz . newInstance();
import org.junit.Test;
import java.lang.reflect.Constructor;
public class Reflection {
@Test
public void TestReflection() throws Exception {
//1、获取Class对象
Class<Student> clazz = Student.class;
//2、触发无参构造
Student student = clazz.newInstance();
System.out.println(student);
//3、触发指定的含参构造
Constructor<Student> c = clazz.getConstructor(String.class, int.class);
Student s = c.newInstance("jack", 17);
System.out.println(s);
}
}