A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 马也keyboard 中级黑马   /  2015-6-23 23:06  /  399 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.beans.BeanInfo;
  2. import java.beans.IntrospectionException;
  3. import java.beans.Introspector;
  4. import java.beans.PropertyDescriptor;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.lang.reflect.Method;
  8. import java.util.Properties;

  9. /**
  10. * 存在一个JavaBean,它包含以下几种可能的属性:

  11.        1:boolean/Boolean
  12.        2:int/Integer
  13.        3:String
  14.        4:double/Double
  15.      属性名未知,现在要给这些属性设置默认值,以下是要求的默认值:
  16.        String类型的默认值为字符串 www.itheima.com
  17.        int/Integer类型的默认值为100
  18.      boolean/Boolean类型的默认值为true
  19.        double/Double的默认值为0.01D.
  20.   只需要设置带有getXxx/isXxx/setXxx方法的属性,非JavaBean属性不设置,请用代码实现
  21. * @author hp
  22. *
  23. */
  24. public class test5 {
  25.        
  26.         public static void property(JavaBean java) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException
  27.         {
  28.                 Class className = java.getClass();
  29.                 BeanInfo bean = Introspector.getBeanInfo(className);
  30.                 PropertyDescriptor[] properties = bean.getPropertyDescriptors();
  31.                
  32.                 for(PropertyDescriptor pd : properties)
  33.                 {
  34.                         String name = pd.getName();
  35.                         Class type = pd.getPropertyType();
  36.                         Method get = pd.getReadMethod();
  37.                         Method set = pd.getWriteMethod();
  38.                        
  39.                         if(!name.equals("class"))
  40.                         {
  41.                                 if(get != null)
  42.                                 {
  43.                                         if(type == boolean.class || type == Boolean.TYPE)
  44.                                         {
  45.                                                 set.invoke(java, true);
  46.                                         }
  47.                                         if(type == int.class || type == Integer.TYPE)
  48.                                         {
  49.                                                 set.invoke(java, 100);
  50.                                         }
  51.                                         if(type == String.class)
  52.                                         {
  53.                                                 set.invoke(java, "www.itheima.com");
  54.                                         }
  55.                                         if(type == double.class || type == Double.TYPE)
  56.                                         {
  57.                                                 set.invoke(java, 0.01d);
  58.                                         }
  59.                                 }
  60.                         }
  61.                 }
  62.         }
  63.         public static void main(String[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException
  64.         {
  65.                 JavaBean java = new JavaBean();
  66.                 property(java);
  67.                 System.out.println(java.getName());
  68.                 System.out.println(java.getNum());
  69.                 System.out.println(java.getFlag());
  70.                 System.out.println(java.getTag());
  71.         }
  72. }

  73. class JavaBean
  74. {
  75.         private String name;
  76.         private int num;
  77.         public boolean flag;
  78.         private double tag;
  79.        
  80.         public String getName() {
  81.                 return name;
  82.         }
  83.         public void setName(String name) {
  84.                 this.name = name;
  85.         }
  86.         public int getNum() {
  87.                 return num;
  88.         }
  89.         public void setNum(int num) {
  90.                 this.num = num;
  91.         }
  92.         public boolean getFlag() {
  93.                 return flag;
  94.         }
  95.         public void setFlag(boolean flag) {
  96.                 this.flag = flag;
  97.         }
  98.         public double getTag() {
  99.                 return tag;
  100.         }
  101.         public void setTag(double tag) {
  102.                 this.tag = tag;
  103.         }
  104.        
  105.        
  106. }
复制代码



在毕老师的视频里没有看到,好像是张老师讲过

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马