简单总结下什么是Java反射,怎么使用。
Java反射机制
反射就是指JVM运行期,获取指定类所有的属性和方法的一种机制。
理解Class类
所有类通过JVM编译后就会生成一个Class对象,生成的对象保存在.class文件中,该文件包含了定义类的相关信息。
Java反射使用
定义一个类
public class ReflectObject {
public String name;
private String username;
public ReflectObject() {
}
@Override
public String toString() {
return "ReflectObject{" +
"name='" + name + '\'' +
", username='" + username + '\'' +
'}';
}
public void addName(String name, int year) {
System.out.println("name:" + name + ",year:" + year);
}
}
获取Class对象三种方法
public class ReflectTest {
public static void main(String[] args) {
// 反射三种创建
// 1.对象获取
ReflectObject object = new ReflectObject();
Class class1 = object.getClass();
System.out.println("class1=" + class1.getName());
// 2.类名
Class class2 = ReflectObject.class;
System.out.println("class2=" + class2.getName());
// 3.具体包名路径
Class class3 = null;
try {
class3 = Class.forName("com.mysiga.learn.proxy.ReflectObject");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("class3=" + class3.getName());
}
}
输出
class1=com.mysiga.learn.proxy.ReflectObject
class2=com.mysiga.learn.proxy.ReflectObject
class3=com.mysiga.learn.proxy.ReflectObject
获取所有方法,成员变量,构造方法
public class ReflectTest {
public static void main(String[] args) {
// 反射三种创建
// 1.对象获取
ReflectObject object = new ReflectObject();
Class class1 = object.getClass();
System.out.println("class1=" + class1.getName());
// 2.类名
Class class2 = ReflectObject.class;
System.out.println("class2=" + class2.getName());
// 3.具体包名路径
Class class3 = null;
try {
class3 = Class.forName("com.mysiga.learn.proxy.ReflectObject");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
System.out.println("class3=" + class3.getName());
// 获取成员方法Method
StringBuilder methodBuilder = new StringBuilder();
Method[] methods = class3.getDeclaredMethods();
// Method[] methods = class3.getMethods();
for (int i = 0; i < methods.length; i++) {
methodBuilder.append(methods[i]).append("\n");
}
System.out.println("所有方法:" + methodBuilder.toString());
// 获取成员变量Field
Field[] fields = class3.getFields();
StringBuilder fieldBuilder = new StringBuilder();
for (int i = 0; i < fields.length; i++) {
fieldBuilder.append(fields[i]).append(",");
}
System.out.println("所有成员变量:" + fieldBuilder.toString());
Method method = null;
try {
method = class3.getMethod("addName", String.class, int.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
if (method != null) {
try {
try {
method.invoke(class3.newInstance(),"Java",12);
} catch (InstantiationException e) {
e.printStackTrace();
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
// 获取构造函数Constructor
StringBuilder constructorBuilder = new StringBuilder();
Constructor<?>[] constructors = class3.getConstructors();
for (int i = 0; i < constructors.length; i++) {
constructorBuilder.append(constructors[i]).append(",");
}
System.out.println("所有构造方法:" + constructorBuilder.toString());
}
}
输出
class1=com.mysiga.learn.proxy.ReflectObject
class2=com.mysiga.learn.proxy.ReflectObject
class3=com.mysiga.learn.proxy.ReflectObject
所有方法:public java.lang.String com.mysiga.learn.proxy.ReflectObject.toString()
public void com.mysiga.learn.proxy.ReflectObject.addName(java.lang.String,int)
所有成员变量:public java.lang.String com.mysiga.learn.proxy.ReflectObject.name,
name:Java,year:12
所有构造方法:public com.mysiga.learn.proxy.ReflectObject(),