java中如何反射獲取一個(gè)類
反射說白了就是可以獲得一個(gè)類的所有信息,主要包括方法和屬性兩部分。
1.獲得方法包括獲得方法的名稱,方法的返回類型,方法的訪問修飾符,以及通過反射執(zhí)行這個(gè)方法。
2.獲得屬性包括屬性的名稱,類型,訪問修飾符,以及這個(gè)屬性的值。
這些獲得都有相應(yīng)的API提供操作。
代碼如下:
package poi; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import org.apache.poi.xwpf.usermodel.XWPFSettings; public class ReflectMain { public static void main(String[] arg) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, ClassNotFoundException, InstantiationException{ XWPFSettings ct = new XWPFSettings(); Class c = ct.getClass(); System.out.println("---------------------指定類的成員變量-----------------------"); System.out.println("反射獲得的類的成員變量個(gè)數(shù)"); System.out.println(c.getDeclaredFields().length); for (Field fil : c.getDeclaredFields()) { System.out.print(fil.getType()+" "); System.out.println(fil.getName()); } System.out.println("------------------------類的構(gòu)造方法-----------------------"); for (Constructor constructor : c.getDeclaredConstructors()) { System.out.print(Modifier.toString(constructor.getModifiers())+" "); System.out.println(constructor.getName()); } System.out.println("--------------------------成員方法--------------------------"); for (Method method : c.getDeclaredMethods()) { System.out.print(Modifier.toString(method.getModifiers())+" "); System.out.print(method.getReturnType()+" "); System.out.println(method.getName()); } System.out.println("---------------------------類的修飾符------------------------"); int mod = c.getModifiers(); String modifier = Modifier.toString(mod); System.out.println("modifier = " + modifier); System.out.println("------------------------指定類的完全限定名--------------------"); System.out.println(c.getName()); System.out.println("------------------------指定類的父類限定名--------------------"); System.out.println(c.getSuperclass().getName()); } }
以上內(nèi)容是本文介紹java中如何反射獲取一個(gè)類的全部?jī)?nèi)容,希望對(duì)大家今后的學(xué)習(xí)有所幫助,同時(shí)也希望與各位大俠共同學(xué)習(xí)、進(jìn)步。
相關(guān)文章
Java線程的start方法回調(diào)run方法的操作技巧
面試過程中經(jīng)常會(huì)被面試官問到為什么我們調(diào)用start()方法時(shí)會(huì)執(zhí)行run()方法,為什么不能直接調(diào)用run()方法,問的一頭霧水,今天小編給大家介紹下Java線程的start方法回調(diào)run方法的操作技巧,需要的朋友參考下吧2017-11-11全面匯總SpringBoot和SpringClould常用注解
Java注解是附加在代碼中的一些元信息,用于一些工具在編譯、運(yùn)行時(shí)進(jìn)行解析和使用,起到說明、配置的功能,這篇文章就帶你來了解一下2021-08-08Java 的雙重分發(fā)與 Visitor 模式實(shí)例詳解
這篇文章主要介紹了Java 的雙重分發(fā)與 Visitor 模式實(shí)例詳解,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07聊聊Java 成員變量賦值和構(gòu)造方法誰先執(zhí)行的問題
這篇文章主要介紹了聊聊Java 成員變量賦值和構(gòu)造方法誰先執(zhí)行的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10Socket+JDBC+IO實(shí)現(xiàn)Java文件上傳下載器DEMO詳解
這篇文章主要介紹了Socket+JDBC+IO實(shí)現(xiàn)Java文件上傳下載器DEMO詳解,需要的朋友可以參考下2017-05-05java多線程之并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore
這篇文章主要為大家介紹了java并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12Java 多線程并發(fā)編程_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了Java 多線程并發(fā)編程的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-05-05SpringBoot整合Thymeleaf與FreeMarker視圖層技術(shù)
在目前的企業(yè)級(jí)應(yīng)用開發(fā)中,前后端分離是趨勢(shì),但是視圖層技術(shù)還占有一席之地。Spring Boot 對(duì)視圖層技術(shù)提供了很好的支持,福安防推薦使用的模板引擎是Thymeleaf,不過想FreeMarker也支持,JSP技術(shù)在這里并不推薦使用2022-08-08