反射Reflection 反射获取类 获取运行时实现的接口及父类 获取构造方法 成员变量 成员方法

mac2026-05-16  6

反射常用的一些方法

getName() 获取名称getSimpleName() 获取简称getModifiers() 获取访问修饰符getParameterTypes() 获取参数列表

反射获取Class类实例的三种方式:

对象.getClass()类名.clsssClass.forName(“全限定名”) newInstance() 创建此 Class 对象所表示的类的一个新实例。 例如:Class<Goods> cls = Goods.class; Goods = cls.newInstance();//调用的是无参构造 第三种和前面两种的区别: 前两种必须明确类型。后面是你只要提供这种类型的字符串就行,这种扩展更强,我不需要知道你的类,我只提供字符串,按照配置文件加载就可以了。 /* * 商品类 */ public class Goods { private String name;//商品名称 private Double price;//商品的价格 private Integer storage;//库存 ....//有参无参构造、get/set方法 } /* * 反射获取Class类实例的三种方式 * 第一种方式: * 利用对象调用getClass()方法获取该对象的Class实例 * 对象.getClass() * 例如:Class cls = new Student().getClass(); * * 第二种方式: * 类名.class * 例如:Class cls = Student.class; * * 第三种方式:这种方式比较常用 推荐 一般开发的时候都是用这种方式 * 使用Class类的静态方法forName("包名.类名"),用类的名字获取一个Class实例。 * Class.forName("全限定名"); * 例如:Class cls = Class.forName("entity.Student"); */ public static void main(String[] args) throws ClassNotFoundException { //获取Goods运行时类 //第三种获取方式 Class<?> cls = Class.forName("demo1.Goods"); //第二种方式 Class cls1 = Goods.class; //第一种方式 //缺点必须创建这个对象,首先有这个类,如果没有直接报错 Class<? extends Goods> cls2 = new Goods().getClass(); }

获取运行时类实现的接口以及父类

(1)获取父类 getSuperclass() (2)获取实现接口 getInterfaces()

//书类 public interface Book { } public class Dog { public Integer gid; public String gname; ....//get/set方法 } public class Goods extends Dog implements Book{ public Integer id;//商品编号 private String name;//商品名称 private Double price;//商品的价格 private Integer storage;//库存 ...//get/set public static void main(String[] args) { //1.获取运行时类 Class cls = Goods.class; //2.获取实现的接口 Class[] interfaces = cls.getInterfaces(); for (Class class1 : interfaces) { //getName()获取名称 String name = class1.getName(); System.out.println(name); //getSimpleName()获取简称 String simpleName = class1.getSimpleName(); System.out.println(simpleName); } //3.获取父类 Class cls2 = cls.getSuperclass(); System.out.println(cls2.getName()); System.out.println(cls2.getSimpleName()); }

通过反射获取构造方法

getConstructor(Class<?>… parameterTypes) 获取指定公共构造

getConstructors() 获取本类所有的公共构造

getDeclaredConstructor(Class<?>… parameterTypes) 获取本类指定的构造(共有或私有)

getDeclaredConstructors() 获取本类所有的构造

创建对象

newInstance() con.newInstance(“zhangsan", 20);

加Declared和不加Declared区别? 加Declared可以获取本类所有的东西,不加Declared获取本类以及父类公共的东西 加s和不加s的区别? 加s获取是多个,返回数组。 不加s返回单个。 public static void main(String[] args) throws Exception { //1.创建运行时类 Class<Goods> cls = Goods.class; System.out.println("------------getConstructors()方法------------"); //2.获取构造 //调用getConstructors() 获取本类所有的公共构造 Constructor[] constructors = cls.getConstructors(); //遍历运行时类里所有的构造 for (Constructor constructor : constructors) { //获取访问修饰符 //getModifiers() 返回此类或接口以整数编码的 Java 语言修饰符。 int modifiers = constructor.getModifiers(); String string = Modifier.toString(modifiers);//将int类型修饰符表现形式转换成字符串表现形式 //获取名称 String name = constructor.getName(); //获取参数列表 Class[] parameterTypes = constructor.getParameterTypes(); for (Class class1 : parameterTypes) { //获取简称 String simpleName = class1.getSimpleName(); System.out.println(simpleName); } System.out.println(string+" "+name); } System.out.println("------------getConstructor(Class<?>... parameterTypes)方法------------"); //获取指定参数的构造 Constructor<Goods> constructor = cls.getConstructor(String.class,Double.class,Integer.class); //创建对象 Goods goods = constructor.newInstance("张三",125.0,18); System.out.println(goods); System.out.println("------------getDeclaredConstructors()方法------------"); Constructor<?>[] declaredConstructors = cls.getDeclaredConstructors(); for (Constructor<?> constructor2 : declaredConstructors) { //获取访问修饰符 int modifiers = constructor2.getModifiers(); String string = Modifier.toString(modifiers);//将int类型修饰符表现形式转换成字符串表现形式 //获取名称 String name = constructor2.getName(); //获取参数列表 Class<?>[] parameterTypes = constructor2.getParameterTypes(); for (Class<?> class1 : parameterTypes) { //简称 System.out.println(class1.getSimpleName()); } System.out.println(string +" "+name); } System.out.println("------------getDeclaredConstructor(Class<?>... parameterTypes)方法------------"); Constructor<Goods> constructor2 = cls.getDeclaredConstructor(String.class,Integer.class); //java.lang.IllegalAccessException: 非法访问异常 constructor2.setAccessible(true);//打破封装,忽略对Java访问修饰符的检查 Goods goods2 = constructor2.newInstance("娃娃",289); System.out.println(goods2); }

通过反射获取运行时类中成员变量

getFields() 获取本类以及父类所有的公共属性getField(String name) 获取本类以及父类中指定的属性(只能获取共有的)getDeclaredFields() 获取本类中所有的属性(共有、私有)getDeclaredField(String name) 获取本类中指定的属性(私有、共有都可以获取) 常用方法: set(Object obj,Object value) 第一个参数:指定对象 第二个参数:需要设置的值 get(Object obj) 返回指定对象上此Field表示的字段的值 第一个参数:指定的对象 public static void main(String[] args) throws Exception { //1.获取运行时类 Goods g = new Goods(); Class<? extends Goods> cls = g.getClass(); Goods goods = new Goods("洗发水", 12.0, 200); System.out.println("--------------getDeclaredFields()------------"); //getDeclaredFields() 获取本类中所有的属性(共有、私有) Field[] fields = cls.getDeclaredFields(); //遍历每一个属性对象 for (Field field : fields) { field.setAccessible(true);//打破封装 //获取访问修饰符 int modifiers = field.getModifiers(); String string = Modifier.toString(modifiers); //获取属性类型 Class<?> type = field.getType(); String simpleName = type.getSimpleName(); //获取属性名称 String name = field.getName(); //获取属性值 Object value = field.get(goods); System.out.println(string+" "+ simpleName+" "+name+" "+value); //设置属性值 //有个问题? 我们在循环里,你给谁设置属性值 if("id".equals(name)){ field.set(goods, 1); }else if("name".equals(name)){ field.set(goods, "洗面奶"); }else if("price".equals(name)){ field.set(goods, 34.0); }else if("storage".equals(name)){ field.set(goods, 1000); } } System.out.println(goods); System.out.println("--------------getDeclaredField(String name)------------"); Field field = cls.getDeclaredField("name"); field.setAccessible(true);//打破封装 //获取访问修饰符 int modifiers = field.getModifiers(); String string = Modifier.toString(modifiers); //获取属性类型 Class<?> type = field.getType(); String simpleName = type.getSimpleName(); //获取名称 String name = field.getName(); //设置属性值 field.set(goods, "洗洁精"); //获取属性值 Object value = field.get(goods); System.out.println(string+" "+ simpleName+" "+name+" "+value); System.out.println("--------------getFields() ------------"); Field[] fields2 = cls.getFields(); for (Field field2 : fields2) { //获取访问修饰符 int modifiers2 = field2.getModifiers(); String string2 = Modifier.toString(modifiers2); //获取属性类型 Class<?> type2 = field2.getType(); String simpleName2 = type2.getSimpleName(); //获取名称 String name2 = field2.getName(); System.out.println(string2 +" "+simpleName2 + " "+name2); } System.out.println("--------------getField(String name) ------------"); Field field2 = cls.getField("gname"); //获取访问修饰符 int modifiers2 = field2.getModifiers(); String string2 = Modifier.toString(modifiers2); //获取属性类型 Class<?> type2 = field2.getType(); String simpleName2 = type2.getSimpleName(); //获取名称 String name2 = field2.getName(); //设置属性值 field.set(goods, "旺财"); //获取属性值 Object val = field.get(goods); System.out.println(string2 +" "+simpleName2 + " "+name2+" "+val); }

通过反射获取成员方法

getDeclaredMethods() 获取本类所有的成员方法(共有、私有)getDeclaredMethod(String name, Class<?>… parameterTypes) 获取本类指定的成员方法(共有、私有)getMethods() 获取本类以及父类所有的公共成员方法getMethod(String name, Class<?>… parameterTypes) 获取本类以及父类指定的公共方法 invoke(Object obj, Object... args) 第一个参数:调用那个对象的方法 第二个参数:需要设置的值 public static void main(String[] args) throws Exception { //1.获取运行时类 Class<Goods> cls = Goods.class; //创建运行时类实例 Goods goods = cls.newInstance(); System.out.println("-------------getDeclaredMethods()方法的演示-------------"); Method[] declaredMethods = cls.getDeclaredMethods(); //循环遍历,获取每个方法 for (Method method : declaredMethods) { method.setAccessible(true);//打破封装 //获取访问修饰符 int modifiers = method.getModifiers(); String string = Modifier.toString(modifiers); //获取返回值类型 Class<?> returnType = method.getReturnType(); String simpleName = returnType.getSimpleName(); //获取方法名称 String name = method.getName(); //获取参数列表 Class<?>[] parameterTypes = method.getParameterTypes(); for (Class<?> class1 : parameterTypes) { //获取简称 System.out.println(class1.getSimpleName()); } System.out.println(string+" "+simpleName+" "+name); //调用方法 //在循环内如果直接调用方法,那每个方法都会被执行,但是我们方法中参数不一样,所以这个如果需要调用方法需判断 if(name.equals("setName")){ //调用方法 //第一个参数:底层调用方法的对象 //第二个参数:用于方法调用的参数 method.invoke(goods, "在坚持坚持就可以用膳了."); } } System.out.println(goods); System.out.println("-------------getDeclaredMethod(String name, Class<?>... parameterTypes)方法-------------"); Method method = cls.getDeclaredMethod("toString"); //获取访问修饰符 int modifiers = method.getModifiers(); String string = Modifier.toString(modifiers); //获取返回值类型 Class<?> returnType = method.getReturnType(); String simpleName = returnType.getSimpleName(); //获取方法名称 String name = method.getName(); System.out.println(string+" "+simpleName+" "+name); //调用方法 Object value = method.invoke(goods, null); System.out.println(value); System.out.println("-------------getMethods()方法-------------"); Method[] methods = cls.getMethods(); for (Method method2 : methods) { //获取访问修饰符 int modifiers2 = method2.getModifiers(); String string2 = Modifier.toString(modifiers2); //获取名称 String name2 = method2.getName(); //获取返回值类型 Class<?> returnType2 = method2.getReturnType(); String simpleName2 = returnType2.getSimpleName(); //获取参数列表 Class<?>[] parameterTypes = method2.getParameterTypes(); for (Class<?> class1 : parameterTypes) { System.out.println(class1.getSimpleName()); } System.out.println(string2+" "+name2+" "+simpleName2); } System.out.println("-------------getMethod(String name, Class<?>... parameterTypes)方法-------------"); Method method2 = cls.getMethod("show", String.class); //获取访问修饰符 int modifiers2 = method2.getModifiers(); String string2 = Modifier.toString(modifiers2); //获取名称 String name2 = method2.getName(); //获取返回值类型 Class<?> returnType2 = method2.getReturnType(); String simpleName2 = returnType2.getSimpleName(); //获取参数列表 Class<?>[] parameterTypes = method2.getParameterTypes(); for (Class<?> class1 : parameterTypes) { System.out.println(class1.getSimpleName()); } System.out.println(string2+" "+name2+" "+simpleName2); //调用方法 method2.invoke(goods, "我真帅"); }
最新回复(0)