Java使用注解和反射簡(jiǎn)化編程的方法示例
本文實(shí)例講述了Java使用注解和反射簡(jiǎn)化編程的方法。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
當(dāng)調(diào)用大量方法,可以使用反射和注解簡(jiǎn)化編程。
二 代碼
import java.lang.annotation.Annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Method; import java.util.ArrayList; @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @interface testAnnoation10 { public String name() default "methodname"; public String unit() default "unit"; } public class ch11_10 { public static void main( String[] args ) throws Exception { ch11_10 ch9 = new ch11_10(); Method method[] = ch9.getClass().getMethods(); for (Method method2 : method) { Annotation annotation = method2.getAnnotation(testAnnoation10.class); Class<?> ts[] = method2.getParameterTypes(); if (method2.getName().indexOf("getData") == -1) continue; ArrayList<Object> params = new ArrayList<Object>(); for (Class<?> class1 : ts) { if (class1.getSimpleName().equals("int")) { params.add(10); } if (class1.getSimpleName().equals("String")) { params.add("100"); } } if (annotation != null) { testAnnoation10 t9 = (testAnnoation10) annotation; System.out.println(t9.name() + " is " + method2.invoke(ch9, params.toArray()) + " " + t9.unit()); } } } @testAnnoation10(name = "SOC", unit = "%") public int getData1( int a ) { return a; } @testAnnoation10(name = "Electricity", unit = "Ah") public String getData2( String b ) { return b; } @testAnnoation10(name = "Tempreture", unit = "AF") public int getData3( int a, int b ) { return a + b; } }
三 運(yùn)行
Tempreture is 20 AF
Electricity is 100 Ah
SOC is 10 %
更多java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java 單向隊(duì)列及環(huán)形隊(duì)列的實(shí)現(xiàn)原理
本文主要介紹了Java 單向隊(duì)列及環(huán)形隊(duì)列的實(shí)現(xiàn)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Idea2023配置JavaWeb項(xiàng)目(最新)
本文將介紹如何配置JavaWeb項(xiàng)目,以在Idea中實(shí)現(xiàn)開(kāi)發(fā)環(huán)境,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09SpringBoot指標(biāo)監(jiān)控功能實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot指標(biāo)監(jiān)控功能實(shí)現(xiàn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)功能
在SpringBoot項(xiàng)目中,結(jié)合MyBatis-Plus(簡(jiǎn)稱(chēng)MP)可以非常方便地實(shí)現(xiàn)分頁(yè)功能,MP為開(kāi)發(fā)者提供了分頁(yè)插件PaginationInterceptor,只需簡(jiǎn)單配置即可使用,本文給大家介紹了SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)功能,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01java 異常之手動(dòng)拋出與自動(dòng)拋出的實(shí)例講解
這篇文章主要介紹了java 異常之手動(dòng)拋出與自動(dòng)拋出的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02