- package com.itheima;
- import java.beans.BeanInfo;
- import java.beans.IntrospectionException;
- import java.beans.Introspector;
- import java.beans.PropertyDescriptor;
- import java.lang.reflect.Method;
- public class Test8
- {
- public static void mian(String [] args) throws Exception
- {
- Class clazz=Class.forName("com.itheima.testBean");
- Object bean=clazz.newInstance();
- BeanInfo bi=Introspector.getBeanInfo(clazz);
- PropertyDescriptor [] pt=bi.getPropertyDescriptors();
- for(PropertyDescriptor pd:pt)
- {
- Object name =pd.getName();
- Object type=pd.getPropertyType();
- Method gt=pd.getReadMethod();
- Method st=pd.getWriteMethod();
- if(!"class".equals(name))
- {
- if(st !=null)
- {
- if(type==boolean.class||type==boolean.class)
- {
- st.invoke(bean, true);
- }
- if(type==String.class)
- {
- st.invoke(bean, "www.itheima.com");
- }
- if(type==int.class||type==Integer.class)
- {
- st.invoke(bean, 100);
- }
- if(type==double.class||type==Double.class)
- {
- st.invoke(bean, 0.01D);
- }
- }
- if(gt !=null)
- {
- System.out.println(type+""+name+"="+gt.invoke(bean, null));
- }
- }
-
- }
- }
- }
- class testBean
- {
- private boolean b;
- private Integer i;
- private String str;
- private Double d;
- public Boolean getb()
- {
- return b;
- }
- public void setb(Boolean b)
- {
- this.b=b;
- }
- public Integer getI()
- {
- return i;
- }
- public void setI(Integer i)
- {
- this.i=i;
- }
- public String getStr()
- {
- return str;
- }
- public void setStr()
- {
- this.str=str;
- }
- public Double getD()
- {
- return d;
- }
- public void setD()
- {
- this.d=d;
- }
-
- }
复制代码
|
|