Java中怎么利用反射獲取類中字段和方法

Java中怎么利用反射獲取類中字段和方法,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司,專注網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、網(wǎng)站營銷推廣,主機(jī)域名網(wǎng)站空間,網(wǎng)站托管、服務(wù)器租用有關(guān)企業(yè)網(wǎng)站制作方案、改版、費(fèi)用等問題,請聯(lián)系創(chuàng)新互聯(lián)。

一、自定義注解

1、字段注解

 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;
 
@Documented
@Target({ElementType.FIELD}) //注解應(yīng)用類型
@Retention(RetentionPolicy.RUNTIME) // 注解的類型
public @interface FieldInterface {
   String name() default "";
}

2、方法注解

 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;
 
@Documented // 標(biāo)記在生成javadoc時(shí)是否將注解包含進(jìn)去,可有可無
@Target({ElementType.METHOD}) //注解應(yīng)用類型
@Retention(RetentionPolicy.RUNTIME) // 注解的類型
public @interface MethodInterface {
   String name() default "";
}

二、創(chuàng)建實(shí)體類

public class TestClass {
 
@FieldInterface(name = "字段注解")
private String name = "ls";
private Constructor<?>[] declaredConstructors;
 
   @MethodInterface(name = "方法注解")
   public void isNot() {
 
   }
}

三、獲取注解值

    //方法注解測試
	public void getMethodAnnotationValue() {
		// 獲取所有的方法
		Method[] name = TestClass.class.getDeclaredMethods();
		for (Method str : name) {
			// 判斷是否方法上存在注解
			boolean annotationPresent = str.isAnnotationPresent(MethodInterface.class);
			if (annotationPresent) {
				// 獲取自定義注解對象
				MethodInterface methodAnno = str.getAnnotation(MethodInterface.class);
				// 根據(jù)對象獲取注解值
				String isNotNullStr = methodAnno.name();		
				System.out.println(isNotNullStr);
			}
		}
	}
 
    //屬性注解測試
	public void getFieldAnnotationValue() throws NoSuchFieldException, SecurityException {
		// 獲取所有的字段
		Field[] fields = TestClass.class.getDeclaredFields();
		for (Field f : fields) {
			// 判斷字段注解是否存在
			boolean annotationPresent2 = f.isAnnotationPresent(FieldInterface.class);
			if (annotationPresent2) {
				FieldInterface name = f.getAnnotation(FieldInterface.class);
				// 獲取注解值
				String nameStr = name.name();
				System.out.println(nameStr);
			}
		}
	}

四、測試

public static void main(String[] args) throws NoSuchFieldException, SecurityException {
     TestClass c = new TestClass();

     // 獲取方法上的注解值
     c.getMethodAnnotationValue();
 
     // 獲取字段上的注解值
     c.getFieldAnnotationValue();
 
}

輸出:

方法注解
字段注解

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。

網(wǎng)頁題目:Java中怎么利用反射獲取類中字段和方法
網(wǎng)頁路徑:http://bm7419.com/article10/jcejgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、移動網(wǎng)站建設(shè)、App開發(fā)、自適應(yīng)網(wǎng)站、響應(yīng)式網(wǎng)站企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司