zoukankan      html  css  js  c++  java
  • 内省 (操作javabean的属性)

     1 package introspector;
     2 
     3 public class person {   //javabean
     4     private String name;//字段
     5     private int age;//字段
     6     private String password;//字段
     7     
     8     public String getName()
     9     {
    10         return name;
    11         
    12     }
    13     public int getAge()
    14     {
    15         return age;
    16         
    17     }
    18     public String getPassword()
    19     {
    20         return password;
    21         
    22     }
    23     public void setName(String name)
    24     {
    25         this.name=name;
    26     }
    27     public void setAge(int age)
    28     {
    29         this.age=age;
    30     }
    31     public void setPassword(String password)
    32     {
    33         this.password=password;
    34     }
    35 
    36 }
     1 package introspector;
     2 
     3 import java.beans.BeanInfo;
     4 import java.beans.IntrospectionException;
     5 import java.beans.Introspector;
     6 import java.beans.PropertyDescriptor;
     7 import java.lang.reflect.Method;
     8 
     9 import org.junit.Test;
    10 
    11 //使用内省api操作bean的属性
    12 public class demo {
    13     //得到bean的所有属性
    14     @Test
    15     public void test1() throws Exception
    16     {
    17         BeanInfo info=Introspector.getBeanInfo(person.class,Object.class);//得到bean自己的属性
    18         PropertyDescriptor[] pds=info.getPropertyDescriptors();
    19         for(PropertyDescriptor pd:pds)
    20         {
    21             System.out.println (pd.getName());
    22         }
    23     }
    24     //操作bean的所有属性
    25         @Test
    26         public void test2() throws Exception
    27         {
    28             person p=new person();
    29             PropertyDescriptor pd=new PropertyDescriptor("age",person.class);
    30             //得到属性的写方法,为属性赋值
    31             Method method=pd.getWriteMethod();
    32             method.invoke(p, 45);
    33             //获取属性的值1
    34             System.out.println(p.getAge());
    35             //获取属性的值2(和一功能一样)
    36             method=pd.getReadMethod();
    37             System.out.println(method.invoke(p, null));
    38             
    39             
    40         }
    41         //高级点内容,获取当前操作的属性的类型
    42                 @Test
    43                 public void test3() throws Exception
    44                 {
    45                     person p=new person();
    46                     PropertyDescriptor pd=new PropertyDescriptor("age",person.class);
    47                     System.out.println(pd.getPropertyType());
    48                     
    49                 }
    50 
    51 }
  • 相关阅读:
    20172332 2017-2018-2 《程序设计与数据结构》第五周学习总结
    ASL测试 课题测试博客
    20172332 2017-2018-2 《程序设计与数据结构》第四周学习总结
    大白话Docker入门(一)
    Hexo博客搭建全解
    代码查重工具sim
    virtual judge 本地部署方案
    POJ题目分类推荐 (很好很有层次感)
    解决Ubuntu下Sublime Text 3无法输入中文
    [pascal入门]数组
  • 原文地址:https://www.cnblogs.com/jjlovemm/p/4250861.html
Copyright © 2011-2022 走看看