Java反射例子汇总 Class Constructor Method Filed
一、反射概述
在平常的开发中Java的反射技术很少被用到,一般我们都是使用公司封装或者开源框架。而反射技术已经被包含到底层框架了,因此我们很少接触到。但是有些框架的原理或者源码如果想读懂就必须要理解并会使用反射技术。例如:EventBus、BufferKnife、android的插件化等等都会用到。理解了反射技术能够帮助我们更快的理解相关框架,也可以增强我们开发人员的基本功。
Java中的反射能做到什么?
1.根据一个字符串的到一个类对象
2.获取一个类的所有的共有、私有方法或者属性(包括静态的)
3.可以对泛型进行反射
二、根据一个字符串得到Class
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 | package com.yw.reflectjavalib; /** * 获取类对象的三种方式 */ public class ReflectDemoTest { /** * 程序的入口,main方法 * * @param args */ public static void main(String[] args) { LogUtil.e( "根据一个字符串获取类全名" ); str2Class(); LogUtil.e( "使用Class.forName获取类全名" ); classForName(); } /** * 根据一个字符串得到一个类 */ public static void str2Class() { String str = "hello world" ; Class classes = str.getClass(); LogUtil.e(classes.getName()); // } /** * 使用ClassForName获取类 */ public static void classForName() { try { Class str = Class.forName( "java.lang.String" ); Class tv = Class.forName( "com.yw.reflectjavalib.LogUtil" ); LogUtil.e(str.getName()); LogUtil.e(tv.getName()); //根据getSupperclass获取超类全名,由于LogUtil的超类是Object,所以Object会被打印 LogUtil.e(str.getSuperclass().getName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 根据类的class属性获取类对象 */ public static void classAtb() { Class c = String. class ; LogUtil.e(c.getName()); LogUtil.e(c.getSuperclass().getName()); } } |
三、反射构造方法,并创建实例
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | package com.yw.reflectjavalib; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; /** * 获取类的成员 * 如:方法、属性、参数 * create by yangwei * on 2020-02-07 14:48 */ public class ClassMemberDemo { /** * 程序的入口main方法 * * @param args */ public static void main(String[] args) { //获取类的构造函数 //获取类的所有构造函数 Book book = new Book(); //获取book类对象 Class bookClass = book.getClass(); //获取book类的所有的构造函数 Constructor[] constructors = bookClass.getDeclaredConstructors(); //打印出构造函数名称、修饰符以及构造函数的参数 for (Constructor c : constructors) { int mod = c.getModifiers(); //获取构造函数的修饰符 LogUtil.e(Modifier.toString(mod) + ":" + c.getName()); Class[] params = c.getParameterTypes(); //获取构造函数的参数 StringBuffer sb = new StringBuffer(); for (Class cs : params) { sb.append(cs.getName()).append( "," ); } LogUtil.e(sb.toString()); } //获取book类的无惨构造函数 try { Constructor noC = bookClass.getDeclaredConstructor(); int mod = noC.getModifiers(); LogUtil.e(Modifier.toString(mod) + ":" + noC.getName()); } catch (NoSuchMethodException e) { e.printStackTrace(); } //获取一个参数的构造函数 try { Constructor oneC = bookClass.getDeclaredConstructor(String. class ); int mod = oneC.getModifiers(); LogUtil.e(Modifier.toString(mod) + ":" + oneC.getName()); } catch (NoSuchMethodException e) { e.printStackTrace(); } //获取有两个参数的构造函数 try { Constructor towC = bookClass.getDeclaredConstructor(String. class , String. class ); int mod = towC.getModifiers(); LogUtil.e(Modifier.toString(mod) + ":" + towC.getName()); } catch (NoSuchMethodException e) { e.printStackTrace(); } //通过Class实例化一个对象 try { Constructor constructor = bookClass.getDeclaredConstructor(); Object object = constructor.newInstance(); Book book1 = (Book) object; book1.setBookName( "《深入Java虚拟机》" ); LogUtil.e(book1.getBookName()); } catch (Exception e) { e.printStackTrace(); } //通过Class.forName实例化加载一个类,并实例化一个对象,并打印书本名称 try { Class classBook = Class.forName( "com.yw.reflectjavalib.Book" ); Constructor constructor = classBook.getDeclaredConstructor(String. class , String. class ); Object object = constructor.newInstance( "《深入Java虚拟机》" , "001" ); Book book2 = (Book) object; LogUtil.e(book2.getBookName()); } catch (Exception e) { e.printStackTrace(); } } } |
四、反射方法(包括静态)
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 | package com.yw.reflectjavalib; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * java反射方法操作 * create by yangwei * on 2020-02-07 16:12 */ public class ReflectMethodDemo { /** * 程序的入口main方法 * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { //获取Book类的私有方法getBookId并执行这个私有方法 Class bookClass = Book. class ; //创建实例 Constructor bookCon = bookClass.getDeclaredConstructor(); Object object = bookCon.newInstance(); //获取这个方法 Method method = bookClass.getDeclaredMethod( "getBookId" , new Class[]{}); //执行这个方法 method.setAccessible( true ); method.invoke(object, new Object[]{}); //使用Class.forName。执行类的静态私有方法 Class bookClass2 = Class.forName( "com.yw.reflectjavalib.Book" ); Method method1 = bookClass2.getDeclaredMethod( "write" ); method1.setAccessible( true ); method1.invoke( null ); //静态方法属于类本身,所以这块执行jinvoke的时候不需要带任何参数 //获取book类的私有实例字段bookName并执行修改 Class bookClass3 = Book. class ; Constructor con3 = bookClass3.getDeclaredConstructor(); Object obj3 = con3.newInstance(); Field field = bookClass3.getDeclaredField( "bookName" ); field.setAccessible( true ); field.set(obj3, "《设计模式》" ); Method method2 = bookClass3.getDeclaredMethod( "getBookName" ); method2.setAccessible( true ); method2.invoke(obj3, new Object[]{}); //后去book的私有静态字段并修改 Class bookClass4 = Book. class ; Constructor con4 = bookClass4.getDeclaredConstructor(); Object obj4 = con4.newInstance(); Field field1 = bookClass4.getDeclaredField( "book" ); field1.setAccessible( true ); field1.set(obj4, "你真坏" ); Object objf = field1.get( null ); LogUtil.e(objf.toString()); } } |
五、反射属性
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 | package com.yw.reflectjavalib; import java.lang.reflect.Field; /** * 对属性进行反射并操作属性 * create by yangwei * on 2020-02-16 17:04 */ public class ReflectFiledDemo { public static void main(String[] args) { // optionFiled(); optionStaticFiled(); } /** * 操作属性 */ public static void optionFiled() { try { Class bookClass = Book. class ; //创建Book实例 Object bookObj = bookClass.newInstance(); //获取book类的bookName属性 Field field = bookClass.getDeclaredField( "bookName" ); field.setAccessible( true ); //获取Field中的值 String bookName = (String) field.get(bookObj); System.out.println(bookName); //设置Field的值 field.set(bookObj, "《大话数据结构》" ); //重新取出值 String bookName2 = (String) field.get(bookObj); System.out.println(bookName2); } catch (Exception e) { e.printStackTrace(); } } /** * 操作静态属性 */ public static void optionStaticFiled() { try { Class bookClass = Book. class ; Field field = bookClass.getDeclaredField( "book" ); field.setAccessible( true ); //获取静态字段book的值 String bookName = (String)field.get( null ); System.out.println(bookName); //设置静态属性book的值 field.set( null , "<深入Java虚拟机>" ); //重新取出值 String bookName2 = (String) field.get( null ); System.out.println(bookName2); } catch (Exception e) { e.printStackTrace(); } } } |
六、获取基本数据类型类对象
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 | package com.yw.reflectjavalib; /** * 基本数据类型获取类对象 * create by yangwei * on 2020-02-07 14:42 */ public class BaseTypeDemo { public static void main(String[] args) { Class bool = Boolean.TYPE; Class mByte = Byte.TYPE; Class mInt = Integer.TYPE; Class mShort = Short.TYPE; Class mLong = Long.TYPE; Class mFloat = Float.TYPE; Class mDouble = Double.TYPE; Class mChart = Character.TYPE; Class mVoid = Void.TYPE; LogUtil.e( "boolean:" + bool.getName()); LogUtil.e( "byte:" + mByte.getName()); LogUtil.e( "int:" + mInt.getName()); LogUtil.e( "short:" + mShort.getName()); LogUtil.e( "long:" + mLong.getName()); LogUtil.e( "float:" + mFloat.getName()); LogUtil.e( "double:" + mDouble.getName()); LogUtil.e( "chart:" + mChart.getName()); //我们常用的void也是有数据类型的,着点大家不要忽略了 LogUtil.e( "void:" + mVoid.getName()); } } |
总结:说句实在话,Java的反射技术真心是晦涩难懂,初学者看着看着可能就懵逼了。为了方便操作在框架层面通常会把Java的反射技术封装成为一个类库,提供给开发者使用。
例如现成的:JOOR。下一节介绍手动封装反射类库(只有一个类),大家可以在做有反射项目的时候直接拿来使用。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探