亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

淺談Java內(nèi)省機制

 更新時間:2022年08月08日 09:53:18   作者:流楚丶格念  
本文主要介紹了Java內(nèi)省機制,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

概念

JavaBean

在實際編程中,我們常常需要一些用來包裝值對象的類,例如Student、 Employee、Order,這些 類中往往沒有業(yè)務(wù)方法,只是為了把需要處理的實體對象進行封裝,有這樣的特征:

  • 屬性都是私有的;
  • 有無參的public構(gòu)造方法;
  • 對私有屬性根據(jù)需要提供公有的getXxx方法以及setXxx方法;

比如:屬性名稱為name,則有g(shù)etName方法返回屬性name值, setName方法設(shè)置name值;注意方法的名稱通常是get或 set加上屬性名稱,并把屬性名稱的首字母大寫;這些方法稱為getters/setters;getters必須有返回值沒有方法參數(shù); setter值沒有返回值,有方法參數(shù);

例如下面的例子:

image-20220807140502129

符合這些特征的類,被稱為JavaBean;

內(nèi)省

內(nèi)省(Inspector)機制就是基于反射的基礎(chǔ), Java語言對Bean類屬性、事件的一種缺省處理方法。

只要類中有g(shù)etXXX方法,或者setXXX方法,或者同時有g(shù)etXXX及setXXX方法,其中g(shù)etXXX方 法沒有方法參數(shù),有返回值; setXXX方法沒有返回值,有一個方法參數(shù);那么內(nèi)省機制就認(rèn)為 XXX為一個屬性;

例如下面代碼

Employee類中根本沒有聲明age屬性,僅僅是聲明了這樣的getter和setter.內(nèi)省機制就認(rèn)為age是屬性

package com.shixun.introspector;

public class Employee {
    private String name;
    private Double score;

    // age將被內(nèi)省認(rèn)為是屬性
    public int getAge(){
        return 30;
    }

    // name將被內(nèi)省認(rèn)為是屬性
    public String getName() {
        return name;
    }

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

    // score將被內(nèi)省認(rèn)為是屬性
    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public static void main(String[] args) {
        

    }
}

相關(guān)API

與Java內(nèi)省有關(guān)的主要類及接口有:

  • java.beans.Introspector類: 為獲得JavaBean屬性、事件、方法提供了標(biāo)準(zhǔn)方法;通常使用其中的getBeanInfo方法返回BeanInfo對象;
  • Java.beans.BeanInfo接口:不能直接實例化,通常通過Introspector類返回該類型對象,提供了返回屬性描述符對象(PropertyDescriptor)、方法描述符對象(MethodDescriptor) 、 bean描述符(BeanDescriptor)對象的方法;
  • Java.beans.PropertyDescriptor類:用來描述一個屬性,該屬性有g(shù)etter及setter方法;

可以使用PropertyDescriptor類的方法獲取屬性相關(guān)的信息,例如getName方法返回屬性的名字:

PropertyDescriptor類中定義了方法可以獲取該屬性的getter和setter方法

方法方法描述
Method getReadMethod()回屬性對應(yīng)的getter方法對象;
Method getWriteMethod()回屬性對應(yīng)的setter方法對象;

下面我們來用代碼深入探究一下:

代碼案例:獲取屬性相關(guān)信息

Employee如上面代碼所示,繼續(xù)編寫主函數(shù)進行測試

首先用BeanInfo接口獲取BeanInfo的對象,再通過BeanInfo對象獲取PropertyDescriptor屬性描述

 //獲取BeanInfo的對象
 BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);
 //通過BeanInfo對象獲取PropertyDescriptor屬性描述
 PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();
 System.out.println("通過Inspector內(nèi)省機制獲取JavaBean屬性======= 打印所有信息 ====================");
 Arrays.stream(propertyDescriptors).forEach(f->{
     System.out.println("====================================");
     System.out.println("屬性名:"+f.getName());
     System.out.println("類型:"+f.getPropertyType());
     System.out.println("get方法:"+f.getReadMethod());
     System.out.println("set方法:"+f.getWriteMethod());
 });

// 或者用增強for
System.out.println("通過Inspector內(nèi)省機制獲取JavaBean屬性======= 打印所有信息 ====================");
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
	System.out.println("====================================");
    System.out.println("名字:" + propertyDescriptor.getName());
    System.out.println("類型:" + propertyDescriptor.getPropertyType());
    System.out.println("get方法:" + propertyDescriptor.getReadMethod());
    System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
}

運行結(jié)果如下:

image-20220807152404930

我們也可以通過反射調(diào)用這里獲取的get或set方法

//創(chuàng)建Employee的對象
Class<?> clazz = Class.forName("com.shixun.introspector.Employee");
Object employee = clazz.newInstance();

//遍歷屬性描述對象
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
    //打印屬性名稱
    System.out.println(propertyDescriptor.getName());
    //判斷屬性名稱是不是name
    if (propertyDescriptor.getName().equals("name")) {
        //setter方法
        Method writeMethod = propertyDescriptor.getWriteMethod();
        //調(diào)用setName方法
        writeMethod.invoke(employee, "jack");
        //getter方法
        Method readMethod = propertyDescriptor.getReadMethod();
        //調(diào)用getName方法
        Object nameValue = readMethod.invoke(employee);
        System.out.println("name屬性的值為:" + nameValue);
    }
    //判斷屬性名稱是否為score
    if (propertyDescriptor.getName().equals("score")) {
        //setter方法
        Method scoreWriteMethod = propertyDescriptor.getWriteMethod();
        //調(diào)用setScore方法
        scoreWriteMethod.invoke(employee, new Double(3000));
        //getter方法
        Method scoreReadMethod = propertyDescriptor.getReadMethod();
        Object scoreValue = scoreReadMethod.invoke(employee);
        System.out.println("score屬性的值為:" + scoreValue);
    }
}
System.out.println("當(dāng)前對象的信息:"+employee.toString());

運行結(jié)果如下所示:

全部代碼附在最下方!?。。。?!

內(nèi)省屬性的注意事項

  • 很多框架都使用了內(nèi)省機制檢索對象的屬性,定義屬性名字時,名字最好起碼以兩個小寫字母開頭,例如stuName,而不要使用sName,某些情況下,可能會導(dǎo)致檢索屬性失??;
  • 內(nèi)省機制檢索屬性時,是根據(jù)getter和setter方法確認(rèn)屬性名字,而不是根據(jù)類里聲明的成員變量名稱決定;

完整代碼

package com.shixun.introspector;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class Employee {
    private String name;
    private Double score;

    // age將被內(nèi)省認(rèn)為是屬性
    public int getAge() {
        return 30;
    }

    // name將被內(nèi)省認(rèn)為是屬性
    public String getName() {
        return name;
    }

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

    // score將被內(nèi)省認(rèn)為是屬性
    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", score=" + score +
                '}';
    }

    public static void main(String[] args) throws ClassNotFoundException, IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
        //獲取BeanInfo的對象
        BeanInfo employeeBeanInfo = Introspector.getBeanInfo(Employee.class);

        //通過BeanInfo對象獲取PropertyDescriptor屬性描述
        PropertyDescriptor[] propertyDescriptors = employeeBeanInfo.getPropertyDescriptors();
//        System.out.println("通過Inspector內(nèi)省機制獲取JavaBean屬性======= 打印所有信息 ====================");
//        Arrays.stream(propertyDescriptors).forEach(f->{
//            System.out.println("====================================");
//            System.out.println("屬性名:"+f.getName());
//            System.out.println("類型:"+f.getPropertyType());
//            System.out.println("get方法:"+f.getReadMethod());
//            System.out.println("set方法:"+f.getWriteMethod());
//        });
//
//
//
//        System.out.println("通過Inspector內(nèi)省機制獲取JavaBean屬性======= 打印所有信息 ====================");
//
//        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
//            System.out.println("名字:" + propertyDescriptor.getName());
//            System.out.println("類型:" + propertyDescriptor.getPropertyType());
//            System.out.println("get方法:" + propertyDescriptor.getReadMethod());
//            System.out.println("set方法:" + propertyDescriptor.getWriteMethod());
//        }

        //創(chuàng)建Employee的對象
        Class<?> clazz = Class.forName("com.shixun.introspector.Employee");
        Object employee = clazz.newInstance();

        //遍歷屬性描述對象
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            //打印屬性名稱
            System.out.println(propertyDescriptor.getName());
            //判斷屬性名稱是不是name
            if (propertyDescriptor.getName().equals("name")) {
                //setter方法
                Method writeMethod = propertyDescriptor.getWriteMethod();
                //調(diào)用setName方法
                writeMethod.invoke(employee, "jack");
                //getter方法
                Method readMethod = propertyDescriptor.getReadMethod();
                //調(diào)用getName方法
                Object nameValue = readMethod.invoke(employee);
                System.out.println("name屬性的值為:" + nameValue);
            }
            //判斷屬性名稱是否為score
            if (propertyDescriptor.getName().equals("score")) {
                //setter方法
                Method scoreWriteMethod = propertyDescriptor.getWriteMethod();
                //調(diào)用setScore方法
                scoreWriteMethod.invoke(employee, new Double(3000));
                //getter方法
                Method scoreReadMethod = propertyDescriptor.getReadMethod();
                Object scoreValue = scoreReadMethod.invoke(employee);
                System.out.println("score屬性的值為:" + scoreValue);
            }
        }

        System.out.println("當(dāng)前對象的信息:"+employee.toString());
    }
}

到此這篇關(guān)于淺談Java內(nèi)省機制的文章就介紹到這了,更多相關(guān)Java內(nèi)省機制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java判斷integer是否為空的詳細(xì)過程

    java判斷integer是否為空的詳細(xì)過程

    在java編寫過程中,我們會使用到各種各樣的表達(dá)式,在使用表達(dá)式的過程中,有哪些安全問題需要我們注意的呢?對java判斷integer是否為空相關(guān)知識感興趣的朋友一起來看看吧
    2023-02-02
  • Spring Boot中單例類實現(xiàn)對象的注入方式

    Spring Boot中單例類實現(xiàn)對象的注入方式

    這篇文章主要介紹了Spring Boot中單例類實現(xiàn)對象的注入方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 實例講解JAVA 模板方法模式

    實例講解JAVA 模板方法模式

    這篇文章主要介紹了JAVA 模板方法模式的的相關(guān)資料,文中示例代碼非常細(xì)致,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • Dubbo異步調(diào)用的實現(xiàn)介紹

    Dubbo異步調(diào)用的實現(xiàn)介紹

    dubbo默認(rèn)使用同步的方式調(diào)用。但在有些特殊的場景下,我們可能希望異步調(diào)用dubbo接口,從而避免不必要的等待時間,這時候我們就需要用到異步。那么dubbo的異步是如何實現(xiàn)的呢?下面就來看看這個問題
    2022-09-09
  • Java 基礎(chǔ):string中的compareTo方法

    Java 基礎(chǔ):string中的compareTo方法

    這篇文章主要介紹了Java 基礎(chǔ):string中的compareTo方法,文章圍繞string中的compareTo方法的相關(guān)資料展開文章詳細(xì)內(nèi)容,希望對待大家有所幫助
    2021-12-12
  • springboot中請求路徑配置在配置文件中詳解

    springboot中請求路徑配置在配置文件中詳解

    這篇文章主要介紹了springboot中請求路徑配置在配置文件中,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java實現(xiàn)遞歸山脈

    Java實現(xiàn)遞歸山脈

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)遞歸山脈,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 淺析SpringBoot2.4 靜態(tài)資源加載問題

    淺析SpringBoot2.4 靜態(tài)資源加載問題

    這篇文章主要介紹了SpringBoot2.4 靜態(tài)資源加載問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Spring容器初始化及問題解決方案

    Spring容器初始化及問題解決方案

    這篇文章主要介紹了Spring容器初始化及問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • Java中clone方法使用筆記

    Java中clone方法使用筆記

    clone顧名思義是復(fù)制,在Java語言中,clone方法被對象調(diào)用,所以會復(fù)制對象,下面這篇文章主要給大家介紹了關(guān)于Java中clone方法使用的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02

最新評論