欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

import java.util.Date;

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

  // 這是 屬性 
  //(get、set開(kāi)頭的方法,getName、setName算一個(gè)屬性,單獨(dú)一個(gè)set或get也算一個(gè)屬性)
  // 屬性名為 去掉get、set后 第一個(gè)大寫(xiě)字母變小寫(xiě)字母。
  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(){ //注意這也是一個(gè)屬性,屬性名為 abc
    return 10;
  }
  /*
  public int getefg(){ //注意這也是一個(gè)屬性,屬性名為 efg
    return 10;
  }*/

  public Date getBirthday() {
    return birthday;
  }

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

}

測(cè)試

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 //這個(gè)是Object類(lèi)里的
      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包
   * 接下來(lái)有更方便的,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中基本類(lèi)型 自動(dòng)轉(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);
  }
  
  /**
   * 一下整個(gè)賦值給 javaBean 對(duì)象,使用 BeanUtils.populate
   * @throws Exception
   */
  private static void test05() throws Exception{
    Student stu = new Student();
    Map m = new HashMap();
    m.put("name", "Lee");//注意:key名一定要與對(duì)象中的變量名一致
    m.put("age", "18");//注意:key名一定要與對(duì)象中的變量名一致
    m.put("birthday", "2020-7-4");//注意:key名一定要與對(duì)象中的變量名一致
    
    ConvertUtils.register(new DateLocaleConverter(), Date.class);
    BeanUtils.populate(stu, m);
    System.out.println(stu.getBirthday());
    
  }
}

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

相關(guān)文章

  • Eclipse+Java+Swing+Mysql實(shí)現(xiàn)電影購(gòu)票系統(tǒng)(詳細(xì)代碼)

    Eclipse+Java+Swing+Mysql實(shí)現(xiàn)電影購(gòu)票系統(tǒng)(詳細(xì)代碼)

    這篇文章主要介紹了Eclipse+Java+Swing+Mysql實(shí)現(xiàn)電影購(gòu)票系統(tǒng)并附詳細(xì)的代碼詳解,需要的小伙伴可以參考一下
    2022-01-01
  • mybatis-plus多表查詢(xún)操作方法

    mybatis-plus多表查詢(xún)操作方法

    這篇文章主要介紹了mybatis-plus多表查詢(xún)操作方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-12-12
  • java中對(duì)list分頁(yè)并顯示數(shù)據(jù)到頁(yè)面實(shí)例代碼

    java中對(duì)list分頁(yè)并顯示數(shù)據(jù)到頁(yè)面實(shí)例代碼

    這篇文章主要介紹了java中對(duì)list分頁(yè)并顯示數(shù)據(jù)到頁(yè)面實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • Java中的CompletableFuture基本用法

    Java中的CompletableFuture基本用法

    這篇文章主要介紹了Java中的CompletableFuture基本用法,CompletableFuture是java.util.concurrent庫(kù)在java 8中新增的主要工具,同傳統(tǒng)的Future相比,其支持流式計(jì)算、函數(shù)式編程、完成通知、自定義異常處理等很多新的特性,需要的朋友可以參考下
    2024-01-01
  • spring(java,js,html) 截圖上傳圖片實(shí)例詳解

    spring(java,js,html) 截圖上傳圖片實(shí)例詳解

    這篇文章主要介紹了spring(java,js,html) 截圖上傳圖片實(shí)例詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-07-07
  • SpringBoot通過(guò)請(qǐng)求對(duì)象獲取輸入流無(wú)數(shù)據(jù)

    SpringBoot通過(guò)請(qǐng)求對(duì)象獲取輸入流無(wú)數(shù)據(jù)

    這篇文章主要介紹了使用SpringBoot通過(guò)請(qǐng)求對(duì)象獲取輸入流無(wú)數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java 實(shí)現(xiàn)對(duì)稱(chēng)加密算法

    Java 實(shí)現(xiàn)對(duì)稱(chēng)加密算法

    這篇文章主要介紹了Java 實(shí)現(xiàn)對(duì)稱(chēng)加密算法的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)Java,感興趣的朋友可以了解下
    2020-11-11
  • 如何查看java進(jìn)程內(nèi)存占用情況

    如何查看java進(jìn)程內(nèi)存占用情況

    這篇文章主要介紹了如何查看java進(jìn)程內(nèi)存占用情況問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-09-09
  • Jar包沖突問(wèn)題原理及解決方案

    Jar包沖突問(wèn)題原理及解決方案

    這篇文章主要介紹了Jar包沖突問(wèn)題原理及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • java instanceof操作符使用及原理解析

    java instanceof操作符使用及原理解析

    這篇文章主要介紹了java instanceof操作符使用及原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12

最新評(píng)論