zoukankan      html  css  js  c++  java
  • 反射--(代码实现)

      创建Student类

     1 public class Student {
     2 
     3     private String name ;
     4     
     5     private int age ;
     6 
     7     @Override
     8     public String toString() {
     9         return "Student [name=" + name + ", age=" + age + "]";
    10     }
    11 }

      创建StudentDemo类

     1 public class StudentDemo {
     2     
     3     public static void main(String[] args) throws Exception {
     4         
     5         // 创建Student对象
     6         Student s = new Student() ;
     7         
     8         // 给s这个对象的name属性赋值为"张三"
     9         PropertiesTools.setProperty(s, "name", "张三") ;
    10         
    11         // 给s这个对象的age属性赋值为23
    12         PropertiesTools.setProperty(s, "age", 23) ;
    13         
    14         // 输出
    15         System.out.println(s);
    16         
    17     }
    18 }

      创建ReflectTest类

     1 import java.lang.reflect.Method;
     2 import java.util.ArrayList;
     3 
     4 public class ReflectTest {
     5     
     6     public static void main(String[] args) throws Exception {
     7         
     8         /**
     9          * 我给你ArrayList<Integer>的一个对象,我想在这个集合中添加一个字符串数据,如何实现呢?
    10          */
    11         // 创建一个ArrayList集合
    12         ArrayList<Integer> al = new ArrayList<Integer>() ;
    13         
    14         /**
    15          * 当我们添加了泛型以后,就规定了该集合只能存储Integer类型的数据,而泛型这种机制只是在编译器有效,到了
    16          * 运行期,这个泛型就不存在了,而这个动作叫做泛型擦除.由此可见在编译期好像完成不了,但是我们可以在运行期
    17          * 对其进行操作,要在运行期完成,就需要使用反射
    18          */
    19         
    20         // 添加
    21         al.add(23) ;
    22         
    23         // 获取字节码文件对象
    24         Class clazz = al.getClass() ;
    25         
    26         // 获取add方法
    27         Method method = clazz.getDeclaredMethod("add", Object.class) ;
    28         
    29         // 调用方法
    30         method.invoke(al, "你好") ;
    31         
    32         // 输出
    33         System.out.println(al);
    34     }
    35 
    36 }

      创建PropertiesTools类

     1 import java.lang.reflect.Field;
     2 
     3 public class PropertiesTools {
     4     
     5     /**
     6      * 作用: 是给obj这个对象的propertyName这个属性设置上value这个值
     7      * @param obj
     8      * @param propertyName
     9      * @param value
    10      * @throws Exception 
    11      */
    12     public static void setProperty(Object obj , String propertyName , Object value) throws Exception{
    13         
    14         // 获取obj这个对象对应的字节码文件对象
    15         Class clazz = obj.getClass() ;
    16         
    17         // 获取propertyName这个属性
    18         Field field = clazz.getDeclaredField(propertyName) ;
    19         
    20         // 取消语法检查
    21         field.setAccessible(true) ;
    22         
    23         // 调用方法赋值
    24         field.set(obj, value) ;
    25     }
    26 }
  • 相关阅读:
    Python 产生和验证IMEI Vevi
    Ubuntu常见问题及解决方案
    error LNK2019: 无法解析的外部符号 WinMain(vs2019)
    NVIDIA GPU 架构演进
    RFCN详解
    Visdom 使用教程
    传统特征提取方法总结
    模型转换、模型压缩、模型加速工具
    YoloV5实战
    React vscode 创建 react 项目流程
  • 原文地址:https://www.cnblogs.com/rongsnow/p/5157148.html
Copyright © 2011-2022 走看看