Java设计模式 - 反射机制
😄 不断学习才是王道
🔥 继续踏上学习之路,学之分享笔记
👊 总有一天我也能像各位大佬一样
🏆 关注我的CSDN:一个有梦有戏的人
👊 打算连载Java设计模式,记录自己的学习心得,分享学习经验。
Java反射技术
反射的应用机制广泛,能够配置类的全限定名(包名 + 类型名)、方法和参数,完成对象的初始化,并且可以大大增强Java的可配置性,SpringIOC的基本原理就是如此。
1、通过反射构建对象
反射构建对象可以是有参和无参。
无参:
先定义ReflectDemo类,并且有个无参数的方法。
1 2 3 4 5
| public class ReflectDemo { public void sayHello() { System.out.println("反射机制!!!"); } }
|
通过反射去构建对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 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类,并且有个含参数的方法以及其构造方法。
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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去创建实例,但是需要注意的是,需要加上参数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 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加粗样式方法完成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| 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、实例:
综合反射对象与方法
无参:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 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; }
|
含参:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| package com.lyd.demo.reflect;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| package com.lyd.demo.reflect;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.lyd.demo.reflect;
public class ReflectTest { public static void main(String[] args) { ReflectDemo.reflect(); ReflectParamDemo.reflect(); } }
|
结果: