zoukankan      html  css  js  c++  java
  • 反射基础练习详解

    java代码

     1 package fanshe;
     2 
     3 import java.lang.reflect.InvocationTargetException;
     4 import java.lang.reflect.Method;
     5 
     6 public class FanSheTest01 {
     7 
     8     /**
     9      * @param args
    10      * @throws Exception 
    11      */
    12     public static void main(String[] args) throws Exception {
    13         // 根据类创建对象
    14         Object obj = createObject("entity.User");
    15         // 根据对象组成获得属性值的Jeson对象
    16         String str = praseJeonByEntity(obj);
    17         System.out.print(str);
    18     }
    19     /**
    20      * 根据对象,将其属性值拼接成Jeson格式
    21      * @throws InvocationTargetException 
    22      * @throws IllegalAccessException 
    23      * @throws IllegalArgumentException 
    24      * */
    25     private static String praseJeonByEntity(Object obj) throws Exception {
    26         StringBuffer sb = new StringBuffer();
    27         Method[] methods = obj.getClass().getDeclaredMethods();
    28         sb.append("{");
    29         for( Method method : methods) {
    30             if( method.getName().startsWith("get")) {
    31                 Object o = method.invoke(obj);
    32                 sb.append(" " "+method.getName().replaceAll("get", "")+" " ");
    33                 sb.append(":");
    34                 sb.append(" " "+o+" " ");
    35                 sb.append(",");
    36             }
    37             
    38         }
    39         sb.deleteCharAt(sb.length()-1);
    40         sb.append("}");
    41         return sb.toString();
    42     }
    43     /**
    44      * 根据类所在的地址创建一个类,根据类创建一个实例
    45      * 对实例属性进行操作
    46      * */
    47     private static Object createObject(String str) throws Exception {
    48         // 加载驱动创建一个类
    49         Class<?> c = Class.forName(str);
    50         // 根据类创建一个实例
    51         Object obj = c.newInstance();
    52         // 找到该类的所有方法
    53         Method[] methods = obj.getClass().getDeclaredMethods();
    54         // 判断方法,进行操作
    55         for( Method method : methods ) {
    56             if( method.getName().equals("setName")) {
    57                 method.invoke(obj, "爱妃");
    58             }
    59             if( method.getName().equals("setAge")) {
    60                 method.invoke(obj, 16);
    61             }
    62         }
    63         return obj;
    64     }
    65 
    66 }
    View Code

    User类

     1 package entity;
     2 
     3 public class User {
     4 
     5     private String name;
     6     private int age;
     7     public String getName() {
     8         return name;
     9     }
    10     public void setName(String name) {
    11         this.name = name;
    12     }
    13     public int getAge() {
    14         return age;
    15     }
    16     public void setAge(int age) {
    17         this.age = age;
    18     }
    19     
    20 }
    View Code
  • 相关阅读:
    2020十一届蓝桥杯-七月省赛题解
    ES6 Generator与C#迭代器
    .NET Core微服务部分知识点
    js点击图片传给一个空的div内放大显示
    Jmeter 中 CSV 如何参数化测试数据并实现自动断言
    安装nvidia-docker2报错E: Unable to locate package nvidia-docker2
    微信小程序-点击事件传递参数
    2020牛客暑期多校训练营(第八场)I Interesting Computer Game K Kabaleo Lite
    Count the Colors
    Basic Gcd Problem
  • 原文地址:https://www.cnblogs.com/cfb513142804/p/4201857.html
Copyright © 2011-2022 走看看