zoukankan      html  css  js  c++  java
  • Java反射实例

    1. package com.whbs.bean;  
    2.    
    3. public class UserBean {  
    4.     private Integer id;  
    5.     private int age;  
    6.     private String name;  
    7.     private String address;  
    8.      
    9.     public UserBean(){  
    10.        System.out.println("实例化");  
    11.     }  
    12.      
    13.     public Integer getId() {  
    14.        return id;  
    15.     }  
    16.     public void setId(Integer id) {  
    17.        this.id = id;  
    18.     }  
    19.     public int getAge() {  
    20.        return age;  
    21.     }  
    22.     public void setAge(int age) {  
    23.        this.age = age;  
    24.     }  
    25.     public String getName() {  
    26.        return name;  
    27.     }  
    28.     public void setName(String name) {  
    29.        this.name = name;  
    30.     }  
    31.     public String getAddress() {  
    32.        return address;  
    33.     }  
    34.     public void setAddress(String address) {  
    35.        this.address = address;  
    36.     }  
    37.      
    38.      
    39.      
    40. }  
    41.    
    42. 2 > 反射测试  
    43.    
    44. package com.whbs.test;  
    45.    
    46. import java.lang.reflect.Field;  
    47. import java.lang.reflect.Method;  
    48.    
    49. import com.whbs.bean.UserBean;  
    50.    
    51. public class Test1 {  
    52.    
    53.     public static void main(String[] args) throws Exception {  
    54.    
    55.         
    56.        /* 
    57.         * 实列化类 方法1 
    58.         */  
    59.        //String classPath = "com.whbs.bean.UserBean";  
    60.        //Class cla = Test1.class.getClassLoader().loadClass(classPath);  
    61.        //Object ob = cla.newInstance();  
    62.         
    63.        /* 
    64.         * 实列化类 方法2 
    65.         */  
    66.        UserBean bean = new UserBean();  
    67.        bean.setId(100);  
    68.        bean.setAddress("武汉");  
    69.         
    70.        //得到类对象  
    71.        Class userCla = (Class) bean.getClass();  
    72.         
    73.        /* 
    74.         * 得到类中的所有属性集合 
    75.         */  
    76.        Field[] fs = userCla.getDeclaredFields();  
    77.        for(int i = 0 ; i < fs.length; i++){  
    78.            Field f = fs[i];  
    79.            f.setAccessible(true); //设置些属性是可以访问的  
    80.            Object val = f.get(bean);//得到此属性的值     
    81.         
    82.            System.out.println("name:"+f.getName()+"  value = "+val);  
    83.             
    84.            String type = f.getType().toString();//得到此属性的类型  
    85.            if (type.endsWith("String")) {  
    86.               System.out.println(f.getType()+" 是String");  
    87.               f.set(bean,"12") ;        //给属性设值  
    88.            }else if(type.endsWith("int") || type.endsWith("Integer")){  
    89.               System.out.println(f.getType()+" 是int");  
    90.               f.set(bean,12) ;       //给属性设值  
    91.            }else{  
    92.               System.out.println(f.getType()+" ");  
    93.            }  
    94.             
    95.        }  
    96.         
    97.         
    98.        /* 
    99.         * 得到类中的方法 
    100.         */  
    101.        Method[] methods = userCla.getMethods();  
    102.        for(int i = 0; i < methods.length; i++){  
    103.            Method method = methods[i];  
    104.            if(method.getName().startsWith("get")){  
    105.               System.out.print("methodName:"+method.getName()+" ");  
    106.               System.out.println("value:"+method.invoke(bean));//得到get 方法的值  
    107.            }  
    108.        }  
    109.     }  
    110.    
  • 相关阅读:
    Lua源码分析(一)二进制块的加载
    Unity的Deferred Shading
    在DirectX12中使用Instancing
    由《怪物弹珠》浅谈游戏的本地化
    浅谈游戏中BOSS设计的思路
    XCOM2中敌对生物设计分析(Aliens篇)
    XCOM2中敌对生物设计分析(ADVENT篇)
    Roguelike元素对游戏设计的影响
    浅谈游戏策划定位
    源于《Unity官方实例教程 “Space Shooter”》思路分析及相应扩展
  • 原文地址:https://www.cnblogs.com/523823-wu/p/7768054.html
Copyright © 2011-2022 走看看