• 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 }
  • 相关阅读:
    SSL 1579——泽泽在巴西
    SSL 1644——取数字问题
    SSL 1589——火车票
    SSL 1506——打鼹鼠
    SSL 1212——大厅安排
    洛谷 1064——金明的预算方案(动态规划的背包问题)
    SSL 1463——公共子串
    SSL 1461——最大连续数列的和
    SSL 1643——最小乘车费用
    SSL 1460——最小代价问题
  • 原文地址:https://www.cnblogs.com/rojas/p/3934751.html
Copyright © 2020-2023  润新知