大家好,我是IT修真院郑州分院第十期学员,一枚正直纯洁善良的JAVA程序员。
今天给大家分享一下关于java反射的基本知识
一、背景介绍
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
反射的最终目的就是要剖析一个类,获得这个类的所有东西,让这个类赤裸裸的站在我们的面前。那么,要想解剖一个类,必须先要获取到该类的字节码文件对象。而解剖使用的就是Class类中的方法.所以我们还是先要获取到每一个字节码文件对应的Class类型的对象。
二、知识剖析(详见代码实战)
1.通过反射获取类的Class对象的三种方式。
(1)类名.class
(2)对象.getClass()
(3) Class.forName("全限定名")
2.通过Class对象获取构造方法。
(1)getConstroctors(所有公有构造方法);
(2)getDeclareConstroctors(所有构造方法);
(3)getConstroctor(获取特定公有构造);
(4)getDeclareConstroctor(获取特定非公有构造)。
3.获取方法(注意入参和返回值)。
(1)getMethods(所有public修饰有方法);
(2)getDeclareMethods(所有方法);
(3)getMethod(获取特定公有方法);
(4)getDeclareMethod(获取特定非公有方法)。
4.获取成员变量并调用(赋值)。
(1)getFields(获取所有公有字段);
(2)getDeclaredFields(获取所有字段,包括:私有、受保护、默认、公有);
(3)getField(获取特定公有字段);
(4)getDeclaredField(获取特定字段,可以是私有的)。
三、常见问题
(1)通过反射获取到类的方法之后,如何操作方法;
(2)通过反射获取到类的成员变量之后,如何给变量赋值。
四、编码实战
(1)反射要操作的student的类;
public class Student {
private Long id;
private String name ;
private Integer qq ;
public Integer number;
protected Date date;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", qq=" + qq +
'}';
}
public void method1(String province){
System.out.println(province);
}
private String method2(String province,Integer num){
System.out.println("province:"+province+";num:"+num);
return province;
}
protected void method3(){
System.out.println("我是受保护的方法");
}
public Student() {
System.out.println("我是student公共无参构造");
}
public Student(Long id, String name, Integer qq) {
this.id = id;
this.name = name;
this.qq = qq;
System.out.println("我是student的公共有参构造");
}
protected Student(String name){
this.name = name;
System.out.println("我是受保护的构造方法");
System.out.println(name);
}
private Student(Integer qq){
this.qq = qq;
System.out.println(qq);
System.out.println("我是私有的构造方法");
}
}
(2)获取Class对象的三种方式(分别是clazz1、clazz2、clazz3);
// 获取class对象
public static void getClazz() throws ClassNotFoundException {
Class clazz1 = Student.class;
System.out.println(clazz1);
System.out.println("---------------------------");
Class clazz2 = Class.forName("Student");
System.out.println(clazz2);
System.out.println("-------------------------------");
Student student = new Student();
Class clazz3 = student.getClass();
System.out.println(clazz3);
}
运行结果:
(3)获取构造方法;
//获取构造方法
public static void getConstructor() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class clazz = Student.class;
System.out.println("----------所有公共构造方法-------------");
Constructor[] pubConstructors = clazz.getConstructors();
for (Constructor constructor:pubConstructors) {
System.out.println(constructor);
}
System.out.println("----------所有的构造方法(公共、受保护、私有)-------------");
Constructor[] allConstructors = clazz.getDeclaredConstructors();
for (Constructor constructor:allConstructors) {
System.out.println(constructor);
}
System.out.println("---------------获取公有的无参构造---------------");
Constructor pubConstructor = clazz.getConstructor();
System.out.println(pubConstructor);
System.out.println("---------------获取私有的有参构造---------------");
Constructor priConstructor = clazz.getDeclaredConstructor(Integer.class);
System.out.println(priConstructor);
priConstructor.setAccessible(true);
Student student = (Student) priConstructor.newInstance(11111);
System.out.println(student);
}
运行结果:
(4)获取方法(并通过反射运行方法);
//获取方法
public static void getMethod() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class clazz = Student.class;
System.out.println("-------------获取公共方法-----------");
Method[] pubMethods = clazz.getMethods();
for (Method method:pubMethods) {
System.out.println(method);
}
System.out.println("-------------获取所有方法(公共、私有、受保护)-----------");
Method[] allMethods = clazz.getDeclaredMethods();
for (Method method:allMethods) {
System.out.println(method);
}
System.out.println("------------------获取指定的公共方法------------");
Method pubMethod = clazz.getMethod("method1", String.class);
System.out.println(pubMethod);
Student student = (Student) clazz.getConstructor().newInstance();
pubMethod.invoke(student,"河南");
System.out.println("------------------获取指定的私有方法------------");
Method priMethod = clazz.getDeclaredMethod("method2", String.class, Integer.class);
System.out.println(priMethod);
priMethod.setAccessible(true);
String province = (String) priMethod.invoke(student,"河南",123);
System.out.println(province);
运行结果:
(5)获取成员变量(并赋值)。
//获取成员变量
public static void getField() throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class clazz = Student.class;
System.out.println("-------------获取所有公有字段-----------------");
Field[] pubFields = clazz.getFields();
for (Field field:pubFields) {
System.out.println(field);
}
System.out.println("-------------获取所有字段(包括私有,受保护的)-----------");
Field[] allFields = clazz.getDeclaredFields();
for (Field field:allFields) {
System.out.println(field);
}
System.out.println("-------------获取公有字段,并调用---------------");
Field pubField = clazz.getField("number");
System.out.println(pubField);
Student student = (Student) clazz.getConstructor().newInstance();
pubField.set(student,123);
System.out.println(student.number);
System.out.println("-------------获取受保护的字段,并调用---------------");
Field proField = clazz.getDeclaredField("date");
System.out.println(proField);
proField.set(student,new Date());
System.out.println(student.date);
}
运行结果:
五、扩展思考
反射在开发过程中的应用应该很广泛,向Spring通过配置文件创建对象就使用了反射的思想,但是具体内部是怎样封装和完成的,还需要我们以后的不断学习。
六、参考文献
本文主要摘自https://blog.csdn.net/sinat_38259539/article/details/71799078。
七、更多讨论
1.反射还有哪些写新特性?
(1)反射main方法;
(2)反射方法的其它使用之---通过反射运行配置文件内容;
(3)反射方法的其它使用之---通过反射越过泛型检查。
2.反射机制实现方法?
class类和reflect类库对反射进行了支持,通过里面的Field、Method和Constructor等类,能够确定对象信息,而在编译时不需要知道类的任何事情。但是反射的.class文件在运行的时候也必须是已知的,.class文件可以在本地也可以通过网络获得。
3.反射是用来干什么的?
一个类有多个组成部分,反射就是加载类,并解析出类的各个组成部分。一般在框架中才会使用反射技术。反射技术是一种打破了常规的私有方法不能被外部访问的特例。但是调用私有方法或字段必须用setAccessible(true)方法,即暴力反射。