新聞中心
今天小編給大家分享一下java怎么獲取反射方法的相關(guān)知識(shí)點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識(shí),所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
成都創(chuàng)新互聯(lián)公司是專業(yè)的廣安網(wǎng)站建設(shè)公司,廣安接單;提供網(wǎng)站建設(shè)、網(wǎng)站制作,網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行廣安網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
1、獲取要反射的方法
獲取反射方法時(shí),有兩個(gè)方法,getMethod 和 getDeclaredMethod。
class Class { @CallerSensitive public Method getMethod(String name, Class>... parameterTypes) throws NoSuchMethodException, SecurityException { Objects.requireNonNull(name); SecurityManager sm = System.getSecurityManager(); if (sm != null) { // 1. 檢查方法權(quán)限 checkMemberAccess(sm, Member.PUBLIC, Reflection.getCallerClass(), true); } // 2. 獲取方法 Method method = getMethod0(name, parameterTypes); if (method == null) { throw new NoSuchMethodException(methodToString(name, parameterTypes)); } // 3. 返回方法的拷貝 return getReflectionFactory().copyMethod(method); } @CallerSensitive public Method getDeclaredMethod(String name, Class>... parameterTypes) throws NoSuchMethodException, SecurityException { Objects.requireNonNull(name); SecurityManager sm = System.getSecurityManager(); if (sm != null) { // 1. 檢查方法是權(quán)限 checkMemberAccess(sm, Member.DECLARED, Reflection.getCallerClass(), true); } // 2. 獲取方法 Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes); if (method == null) { throw new NoSuchMethodException(methodToString(name, parameterTypes)); } // 3. 返回方法的拷貝 return getReflectionFactory().copyMethod(method); } }
2、在Java5中,提供了for-each循環(huán),從而簡(jiǎn)化了對(duì)數(shù)組和集合的循環(huán)。Fore-each循環(huán)允許您遍歷數(shù)組而不需要保留傳統(tǒng)for循環(huán)中的索引,也不需要在使用迭代器時(shí)調(diào)用while循環(huán)中的hasNext方法和next方法來遍歷集合。
double[] values = ...; for(double value : values) { // TODO: 處理value } ListvalueList = ...; for(Double value : valueList) { // TODO: 處理value }
3、得到當(dāng)前方法的名字
String methodName = Thread.currentThread().getStackTrace()[
1
].getMethodName();
以上就是“java怎么獲取反射方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會(huì)為大家更新不同的知識(shí),如果還想學(xué)習(xí)更多的知識(shí),請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前文章:java怎么獲取反射方法
標(biāo)題URL:http://fisionsoft.com.cn/article/ijcsgc.html