最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java 內(nèi)省introspector相關(guān)原理代碼解析

 更新時間:2020年07月29日 09:20:55   作者:htj10  
這篇文章主要介紹了Java 內(nèi)省introspector相關(guān)原理代碼解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

1. JavaBean (有g(shù)et/set屬性,和默認(rèn)構(gòu)造器等規(guī)范的java類)

import java.util.Date;

public class Student {
  // 這是 字段
  private String name;
  private int age;
  private Date birthday;

  // 這是 屬性 
  //(get、set開頭的方法,getName、setName算一個屬性,單獨(dú)一個set或get也算一個屬性)
  // 屬性名為 去掉get、set后 第一個大寫字母變小寫字母。
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }
  
  public int getAbc(){ //注意這也是一個屬性,屬性名為 abc
    return 10;
  }
  /*
  public int getefg(){ //注意這也是一個屬性,屬性名為 efg
    return 10;
  }*/

  public Date getBirthday() {
    return birthday;
  }

  public void setBirthday(Date birthday) {
    this.birthday = birthday;
  }

}

測試

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;

public class Test1 {
  public static void main(String[] args) throws Exception {
    test05();
  }

  // 獲取屬性描述器 Introspector.getBeanInfo(Student.class).getPropertyDescriptors();
  private static void test01() throws Exception {
    BeanInfo bf = Introspector.getBeanInfo(Student.class);
    PropertyDescriptor[] pds = bf.getPropertyDescriptors();
    for (PropertyDescriptor pd : pds) {
      System.out.println(pd.getName());
    }
    /*
      abc
      age
      class //這個是Object類里的
      name
     */
  }
  
  // 使用內(nèi)省 調(diào)用set、get方法
  private static void test02() throws Exception {
    Student stu = new Student();
    PropertyDescriptor pd = new PropertyDescriptor("name", Student.class);
    Method setter = pd.getWriteMethod();
    setter.invoke(stu, "tom");
    Method getter = pd.getReadMethod();
    System.out.println(getter.invoke(stu));
  }
  
  
  /**
   * 以上使用的 java源碼里的 java.beans包
   * 接下來有更方便的,Apache 組織提供的 commons-beanutils-1.8.3.jar 
   * 導(dǎo)入:commons-beanutils-1.8.3.jar commons-logging-1.1.1.jar
   */
  private static void test03() throws Exception{
    Student stu = new Student();
    BeanUtils.setProperty(stu, "name", "白居易");
    System.out.println(stu.getName());
    String name = BeanUtils.getProperty(stu, "name");
    System.out.println(name);
    //BeanUtils 支持8中基本類型 自動轉(zhuǎn)換
    BeanUtils.setProperty(stu, "age", 19);
    BeanUtils.setProperty(stu, "age", "18");
    System.out.println(stu.getAge());
    //PropertyUtils.setSimpleProperty(stu, name, value);
  }
  
  private static void test04() throws Exception{
    Student stu = new Student();
    //set/get 日期 Date
    ConvertUtils.register(new DateLocaleConverter(), Date.class);
    BeanUtils.setProperty(stu, "birthday", "1999-11-10");
    System.out.println(stu.getBirthday());
    String s = BeanUtils.getProperty(stu, "birthday");
    System.out.println(s);
  }
  
  /**
   * 一下整個賦值給 javaBean 對象,使用 BeanUtils.populate
   * @throws Exception
   */
  private static void test05() throws Exception{
    Student stu = new Student();
    Map m = new HashMap();
    m.put("name", "Lee");//注意:key名一定要與對象中的變量名一致
    m.put("age", "18");//注意:key名一定要與對象中的變量名一致
    m.put("birthday", "2020-7-4");//注意:key名一定要與對象中的變量名一致
    
    ConvertUtils.register(new DateLocaleConverter(), Date.class);
    BeanUtils.populate(stu, m);
    System.out.println(stu.getBirthday());
    
  }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

长子县| 隆安县| 左权县| 察哈| 高青县| 绥滨县| 佳木斯市| 荔波县| 昭觉县| 大足县| 常宁市| 安西县| 页游| 岳阳市| 景德镇市| 浪卡子县| 朝阳县| 资溪县| 板桥市| 蒙山县| 苏尼特右旗| 台山市| 偃师市| 且末县| 宜兰县| 开鲁县| 巧家县| 大余县| 西丰县| 海宁市| 大悟县| 西吉县| 云霄县| 吕梁市| 茌平县| 金山区| 武川县| 彝良县| 牙克石市| 涟源市| 菏泽市|