• Introspector


     1 import java.beans.BeanInfo;
     2 import java.beans.Introspector;
     3 import java.beans.PropertyDescriptor;
     4 import java.lang.reflect.Method;
     5 public class IntrospectorDemo {
     6 
     7     public static void main(String[] args) {
     8         // TODO Auto-generated method stub
     9         IntrospectorDemo id=new IntrospectorDemo();
    10         try {
    11             id.test1();
    12             id.test2();
    13             id.test3();
    14             id.test4();
    15         } catch (Exception e) {
    16             // TODO Auto-generated catch block
    17             e.printStackTrace();
    18         }
    19     }
    20     
    21 
    22     public class Person
    23     {
    24         public Person(){
    25         }
    26         public Person(String name,int age){
    27             this.name=name;
    28             this.age=age;
    29         }
    30         private String name;
    31         private int age;
    32         public String getName() {
    33             return name;
    34         }
    35         public void setName(String name) {
    36             this.name = name;
    37         }
    38         public int getAge() {
    39             return age;
    40         }
    41         public void setAge(int age) {
    42             this.age = age;
    43         }
    44         
    45     }
    46      
    47         //获得person类的所有bean属性
    48          
    49         public void test1() throws Exception{
    50             BeanInfo info = Introspector.getBeanInfo(Person.class);
    51             PropertyDescriptor[] pds = info.getPropertyDescriptors();
    52             for(PropertyDescriptor pd:pds){
    53                 System.out.println(pd.getPropertyType()+" "+pd.getName());
    54             }
    55         }
    56         //获得Person(不包含Object类)的所有bean属性
    57          
    58         public void test2() throws Exception{
    59             BeanInfo info = Introspector.getBeanInfo(Person.class,Object.class);
    60             PropertyDescriptor[] pds = info.getPropertyDescriptors();
    61             for(PropertyDescriptor pd:pds){
    62                 System.out.println(pd.getPropertyType()+" "+pd.getName());
    63             }
    64         }
    65         //设置Person的age属性
    66          
    67         public void test3() throws Exception{
    68             Person p = new Person();
    69             PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
    70             Method method = pd.getWriteMethod();
    71             method.invoke(p, 20);
    72             System.out.println(p.getAge());
    73         }
    74         //调用Person的getAge()
    75          
    76         public void test4() throws Exception{
    77             Person p = new Person("xiazdong",30);
    78             PropertyDescriptor pd = new PropertyDescriptor("age", Person.class);
    79             Method method = pd.getReadMethod();
    80             System.out.println(method.invoke(p, null));
    81         }
    82      
    83 }
  • 相关阅读:
    逻辑实现与物理实现
    逻辑实现与物理实现
    可逆矩阵的逆
    可逆矩阵的逆
    算法 Tricks(三)—— 判断序列是否为等差数列
    算法 Tricks(三)—— 判断序列是否为等差数列
    C++ Tricks(一)—— 判断字符串 string 对象的所有字符都相等
    AndroidMainifest标签使用说明3——<activity-alias>
    支持向量机通俗导论(理解SVM的三层境地)
    iOS中,MRC和ARC混编
  • 原文地址:https://www.cnblogs.com/rojas/p/3934751.html
Copyright © 2020-2023  润新知