目录
三种获得字节码对象的方法:
首先创建一个学生类:
该学生类会用于以下所有的演示中
package com.Demo01;
public class Student {
// 成员变量:一个私有,一个默认,两个公共
private String name;
int age;
public String address;
// 构造方法:一个私有,一个默认,两个公共
public Student() {
}
private Student(String name) {
this.name = name;
}
Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
// 成员方法:一个私有,四个公共
private void function(){
System.out.println("function");
}
public void method1(){
System.out.println("method");
}
public void method2(String s){
System.out.println("method:" + s);
}
public String method3(String s, int i){
return s + "," + i;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
然后创建一个测试类,实现三种字节码对象的获取:
以下所有的forName方法地址都需要自己输入自己的实际地址
以上就是笔者Student类放置的位置,可根据自己的放置位置模仿笔者书写地址。
package com.Demo01;
/**
* 三种方式获得字节码文件对象
*/
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException {
// 使用类的class属性来获取该类对应的class对象
Class<Student> c1 = Student.class; // 获得Student类的字节码文件对象
System.out.println(c1);
Class<Student> c2 = Student.class;
// 一个类的字节码文件对象只有一个,所以此处应该相等
System.out.println(c1 == c2);
System.out.println("======================");
// 调用对象的getClass()方法,返回该对象所属类对应的Class对象
Student s = new Student();
Class<? extends Student> c3 = s.getClass();
System.out.println(c1 == c3);
System.out.println("=====================");
// 使用Class类中的静态方法farName(String className)
Class<?> c4 = Class.forName("com.Demo01.Student");
}
}
反射获取构造方法的使用:
具体类的实现可以查看API中Class类中的方法:
package com.Demo02;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
/**
* 反射获取构造方法的使用
*/
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
// 获取Class对象
Class<?> c = Class.forName("com.Demo01.Student");
//Constructor<?>[] getConstructors()
//返回包含一个数组 Constructor对象反射由此表示的类的所有公共构造 类对象。
Constructor<?>[] cons = c.getConstructors();
// 这里私有构造方法和默认构造方法未显示
for(Constructor<?> con:cons){
System.out.println(con);
}
//Constructor<?>[] getDeclaredConstructors()
//返回一个反映 Constructor对象表示的类声明的所有 Constructor对象的数组 类 。
// 该方法可以返回所有的构造方法,可以显示默认和私有方法
System.out.println("============");
Constructor<?>[] decl = c.getDeclaredConstructors();
for(Constructor<?> dec : decl){
System.out.println(dec);
}
System.out.println("============");
// Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
//返回一个 Constructor对象,该对象反映 Constructor对象表示的类或接口的指定 类函数。
// 参数:你要获取的构造方法的参数的个数和数据类型对应的字节码文件对象
Constructor<?> con = c.getConstructor();
// 创建对象信息
Object obj = con.newInstance();
System.out.println(obj);
}
}
反射获取成员变量并使用:
package com.Demo03;
import com.Demo01.Student;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
/**
* 反射获取成员变量并使用
*/
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
// 获取Class对象
Class<?> c = Class.forName("com.Demo01.Student");
//Field[] getFields()
//返回包含一个数组 Field对象反射由此表示的类或接口的所有可访问的公共字段 类对象。
//Field[] getDeclaredFields()
//返回的数组 Field对象反映此表示的类或接口声明的所有字段 类对象。
Field[] fields = c.getFields();
for (Field field : fields) { // 看到公共成员变量
System.out.println(field);
}
System.out.println("==============");
Field[] declaredFields = c.getDeclaredFields();
for (Field field : declaredFields) { // 看到私有、默认、公共成员变量
System.out.println(field);
}
System.out.println("================");
//Field getField(String name)
//返回一个 Field对象,它反映此表示的类或接口的指定公共成员字段 类对象。
//Field getDeclaredField(String name)
//返回一个 Field对象,它反映此表示的类或接口的指定已声明字段 类对象。
Field addressField = c.getField("address");
// 获取无参构造方法创建对象
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
// obj.addressField = "西安";
addressField.set(obj,"西安"); // 给obj的成员变量addressField赋值为西安
System.out.println(obj);
/*
Student s = new Student();
s.address = "西安";
System.out.println(s);
*/
}
}
反射获取成员方法并使用:
package com.Demo04;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 反射获取成员方法并使用
*/
public class ReflectDemo {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
// 获取Class对象
Class<?> c = Class.forName("com.Demo01.Student");
// Methods[] getMethods()
//返回包含一个数组 方法对象反射由此表示的类或接口的所有公共方法 类对象,包括那些由类或接口和那些从超类和超接口继承的声明。
//Methods[] getDeclaredMethods()
//返回包含一个数组 方法对象反射的类或接口的所有声明的方法,通过此表示 类对象,包括公共,保护,默认(包)访问和私有方法,但不包括继承的方法。
Method[] methods = c.getMethods();
// 包括继承的方法,不包括私有方法
for (Method method : methods){
System.out.println(method);
}
System.out.println("=============");
Method[] declaredMethods = c.getDeclaredMethods();
// 不包括继承的方法,包括私有对象
for (Method dec : declaredMethods){
System.out.println(dec);
}
System.out.println("==============");
//方法 getMethod(String name, 类<?>... parameterTypes)
//返回一个 方法对象,它反映此表示的类或接口的指定公共成员方法 类对象。
// 方法 getDeclaredMethod(String name, 类<?>... parameterTypes)
//返回一个 方法对象,它反映此表示的类或接口的指定声明的方法 类对象。
Method m = c.getMethod("method1");
// 获取无参构造方法创建对象
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
m.invoke(obj);
}
}
反射的部分应用实例:
采用反射方法越过泛型检查
package com.Demo05;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
/**
* 反射练习之越过泛型检查
* 题目:有一个ArrayList<Integer>集合,现在我想对在这个集合中添加一个字符串数据,如何实现?
*/
public class ReflectDemo {
public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
ArrayList<Integer> array = new ArrayList<Integer>();
// 添加数字时正常,添加字符串由于类型不合适导致错误
// array.add(10);
// array.add(20);
// array.add("asdad");
// 采用反射解决
// 反射方法可以越过泛型检查,获得原始方法
Class<? extends ArrayList> c = array.getClass();
Method m = c.getMethod("add", Object.class);
m.invoke(array,"hello");
m.invoke(array,"word");
m.invoke(array,"java");
System.out.println(array);
}
}
运行配置文件指定内容:
Student类:
package com.Demo06;
public class Student {
public void study(){
System.out.println("好好学习天天向上");
}
}
Teacher类:
package com.Demo06;
public class Teacher {
public void teach(){
System.out.println("用爱成就学员");
}
}
配置文件:
className=com.Demo06.Student
methodName=study
测试类:
package com.Demo06;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;
/**
* 运行配置文件指定内容
*/
public class ReflectDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
// Student s = new Student();
// s.study();
// Teacher t = new Teacher();
// t.teach();
// 通过反射实现对运行配置文件的配置
// 这样可以不用怕频繁修改main方法
/*
class.txt
className=xxx
methodName=xxx
*/
// 加载数据
Properties prop = new Properties();
FileReader fr = new FileReader("src/com/Demo06/class.txt");
prop.load(fr);
fr.close();
/*
className=com.Demo06.Student
methodName=study
*/
String className = prop.getProperty("className");
String methodName = prop.getProperty("methodName");
// 通过反射来使用
Class<?> c = Class.forName(className);
Constructor<?> con = c.getConstructor();
Object obj = con.newInstance();
Method m = c.getMethod(methodName);//study
m.invoke(obj);
}
}
只需要根据调整配置文件即可,不需要改动代码即可以实现Student和Teacher之间的实现转换。