Java设计模式 - 反射机制
不断学习才是王道
继续踏上学习之路,学之分享笔记
总有一天我也能像各位大佬一样
一个有梦有戏的人
Java反射技术
反射的应用机制广泛,能够配置类的全限定名(包名 + 类型名)、方法和参数,完成对象的初始化,并且可以大大增强Java的可配置性,SpringIOC的基本原理就是如此。
1、通过反射构建对象
反射构建对象可以是有参和无参。
无参:
先定义ReflectDemo类,并且有个无参数的方法。
public class ReflectDemo {
public void sayHello() {
System.out.println("反射机制!!!");
}
}
通过反射去构建对象
// 通过反射去构建
public ReflectDemo getInstance() {
ReflectDemo object = null;
try {
object = (ReflectDemo) Class.forName("com.lyd.demo.reflect.ReflectDemo").newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return object;
}
其实就是通过Class的forName()去找到这个类,在通过newInstance()方法创建实例。
给类加载器去注册了一个类ReflectDemo的全限定名
object = (ReflectDemo) Class.forName("com.lyd.demo.reflect.ReflectDemo").newInstance();
含参:
先定义ReflectParamDemo类,并且有个含参数的方法以及其构造方法。
public class ReflectParamDemo {
private String name;
private int age;
public ReflectParamDemo(String name, int age) {
this.name = name;
this.age = age;
}
public void sayHello(String name, int age) {
System.out.println("名字: " + name + " 年龄: " + age);
}
}
同样,根据Class.forName去创建实例,但是需要注意的是,需要加上参数。
// 通过反射去构建
public ReflectParamDemo getInstance() {
ReflectParamDemo object = null;
try {
object = (ReflectParamDemo) Class.forName("com.lyd.demo.reflect.ReflectParamDemo")
.getConstructor(String.class, int.class)
.newInstance("李四", 18);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return object;
}
getConstructor(),带入参数类型
object = (ReflectParamDemo) Class.forName("com.lyd.demo.reflect.ReflectParamDemo")
.getConstructor(String.class, int.class)
.newInstance("李四", 18);
2、反射方法
无参:
暂时先演示反射方法,所以用new的方法来创建实例target,具体的代码请看实例, 通过ReflectDemo.class.getMethod("sayHello");可以获取到类中的方法,并且通过invoke加粗样式方法完成。
//反射方法
public Object reflectMethod() {
Object retuenObj = null;
ReflectDemo target = new ReflectDemo();
try {
Method method = ReflectDemo.class.getMethod("sayHello");
retuenObj = method.invoke(target);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return retuenObj;
}
target: 指定对象
invoke(target):调用哪个对象中的方法,如果是含参的,可以在后面添加参数,可以是多个参数
3、实例:
综合反射对象与方法
无参:
//实例
public static Object reflect() {
ReflectDemo object = null;
try {
object = (ReflectDemo) Class.forName("com.lyd.demo.reflect.ReflectDemo").newInstance();
Method method = ReflectDemo.class.getMethod("sayHello");
method.invoke(object);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return object;
}
含参:
//实例
public static Object reflect() {
ReflectParamDemo object = null;
try {
object = (ReflectParamDemo) Class.forName("com.lyd.demo.reflect.ReflectParamDemo")
.getConstructor(String.class, int.class)
.newInstance("李四", 18);
Method method = ReflectParamDemo.class.getMethod("sayHello", String.class, int.class);
method.invoke(object, "李四", 18);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return object;
}
总体代码
ReflectDemo:
package com.lyd.demo.reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @Author: lyd
* @Description: 反射机制-无参数
* @Date: 2022-08-14
*/
public class ReflectDemo {
public void sayHello() {
System.out.println("反射机制!!!");
}
// 通过反射去构建
public ReflectDemo getInstance() {
ReflectDemo object = null;
try {
object = (ReflectDemo) Class.forName("com.lyd.demo.reflect.ReflectDemo").newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return object;
}
//反射方法
public Object reflectMethod() {
Object retuenObj = null;
ReflectDemo target = new ReflectDemo();
try {
Method method = ReflectDemo.class.getMethod("sayHello");
retuenObj = method.invoke(target);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return retuenObj;
}
//实例
public static Object reflect() {
ReflectDemo object = null;
try {
object = (ReflectDemo) Class.forName("com.lyd.demo.reflect.ReflectDemo").newInstance();
Method method = ReflectDemo.class.getMethod("sayHello");
method.invoke(object);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return object;
}
}
ReflectParamDemo:
package com.lyd.demo.reflect;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @Author: lyd
* @Description: 反射机制-带参数
* @Date: 2022-08-14
*/
public class ReflectParamDemo {
private String name;
private int age;
public ReflectParamDemo(String name, int age) {
this.name = name;
this.age = age;
}
public void sayHello(String name, int age) {
System.out.println("名字: " + name + " 年龄: " + age);
}
// 通过反射去构建
public ReflectParamDemo getInstance() {
ReflectParamDemo object = null;
try {
object = (ReflectParamDemo) Class.forName("com.lyd.demo.reflect.ReflectParamDemo")
.getConstructor(String.class, int.class)
.newInstance("李四", 18);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return object;
}
//实例
public static Object reflect() {
ReflectParamDemo object = null;
try {
object = (ReflectParamDemo) Class.forName("com.lyd.demo.reflect.ReflectParamDemo")
.getConstructor(String.class, int.class)
.newInstance("李四", 18);
Method method = ReflectParamDemo.class.getMethod("sayHello", String.class, int.class);
method.invoke(object, "李四", 18);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return object;
}
}
测试:ReflectTest
package com.lyd.demo.reflect;
/**
* @Author: lyd
* @Description: 反射机制测试
* @Date: 2022-08-14
*/
public class ReflectTest {
public static void main(String[] args) {
ReflectDemo.reflect();
ReflectParamDemo.reflect();
}
}