A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. package com.reflect;

  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.Field;

  4. public class MyReflect {

  5.         public static void main(String[] args) throws Exception {
  6.                 // ReflectPoint re = new ReflectPoint(3, 5);
  7.                 // 通过反射拿到和该类相关联的构造方法类,指定参数是明确该类的哪个构造方法
  8.                 Constructor constructor = ReflectPoint.class.getConstructor(int.class,
  9.                                 int.class);
  10.                 // 获取和该构造方法相关联的类
  11.                 Class c = constructor.getDeclaringClass();
  12.                 System.out.println(c);

  13.                 ReflectPoint re2 = (ReflectPoint) constructor.newInstance(6, 8);
  14.                 Field fieldY = re2.getClass().getField("y");
  15.                 Field fieldX = re2.getClass().getDeclaredField("x");

  16.                 fieldX.setAccessible(true);
  17.                 int y = (Integer) fieldY.get(re2);
  18.                 int x = (Integer) fieldX.get(re2);
  19.                 System.out.print(y);
  20.                 System.out.print(x);
  21.                 /*
  22.                  * 以下是三种获取Classl类的方法
  23.                  */

  24.                 Field fields1 = re2.getClass().getDeclaredField("s1");
  25.                 // Field fields1=ReflectPoint.class.getDeclaredField("s1");
  26.                 // Field fields1 = Class.forName("com.reflect.ReflectPoint")
  27.                 // .getDeclaredField("s1");

  28.                 fields1.setAccessible(true);// 暴力反射,强制该类私有属性可见
  29.                 // 这个比较简便获取类的实例对象,但该类必须有一个无参的构造方法
  30.                 // ReflectPoint ref = ReflectPoint.class.newInstance();
  31.                 // 如果该类没有无参的构造方法,且具备有参数的构造方法,用下面这两行代码获取类实例,即通过拿到构造方法对象con,通过con的NewInstance方法

  32.                 /*
  33.                  * Constructor
  34.                  * con=ReflectPoint.class.getConstructor(int.class,int.class);
  35.                  * ReflectPoint ref=(ReflectPoint) con.newInstance(9,9);
  36.                  */

  37.                 // 和该类相关联的属性类通过get(该类对象),拿到属性值
  38.                 // String s = (String) fields1.get(ref);
  39.                 // System.out.println(s);
  40.                 changeValue(re2);
  41.                 System.out.print(re2);
  42.         }

  43.         private static void changeValue(Object obj) throws Exception {
  44.                 // 拿到和这个类相关的所有变量类
  45.                 Field[] fields = obj.getClass().getFields();
  46.                 // 遍历变量类
  47.                 for (Field f : fields) {
  48.                         // 如果变量类的字节码与String的字节码相同
  49.                         if (f.getType() == String.class) {
  50.                                 // 则通过get(这个类对象)拿到变量值
  51.                                 String s = (String) f.get(obj);
  52.                                 String newValue = s.replace('b', 't');
  53.                                 f.set(obj, newValue);
  54.                         }
  55.                 }

  56.         }

  57. }
复制代码
希望大家多多交流,指出理解的误区

2 个回复

倒序浏览
这好像是视频的代码,你是怎么理解的呢?
回复 使用道具 举报
加油,,正在努力学习中。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马