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 }
  • 相关阅读:
    linux启动流程
    树-二叉平衡树AVL
    算法导论第六章 堆排序
    算法导论基础(第一~五章)
    树-二叉查找树
    Java:基础
    【转】为什么C++编译器不能支持对模板的分离式编译
    压缩和解压缩命令
    Makefile编程
    1.什么是Mybatis?
  • 原文地址:https://www.cnblogs.com/jjlovemm/p/4250861.html
Copyright © 2011-2022 走看看