- java.lang.reflect 程式包類
- java.lang.reflect - 主頁
- java.lang.reflect - AccessibleObject
- java.lang.reflect - Array
- java.lang.reflect - Constructor<T>
- java.lang.reflect - Field
- java.lang.reflect - Method
- java.lang.reflect - Modifier
- java.lang.reflect - Proxy
- java.lang.reflect 程式包補充功能
- java.lang.reflect - 介面
- java.lang.reflect - 異常
- java.lang.reflect - 錯誤
- java.lang.reflect 有用資源
- java.lang.reflect - 快速指南
- java.lang.reflect - 有用資源
- java.lang.reflect - 討論
java.lang.reflect.Constructor.getDeclaredAnnotations() 方法示例
說明
java.lang.reflect.Constructor.getDeclaredAnnotations() 方法返回直接出現在此元素上的所有註釋。與該介面中的其他方法不同,此方法不考慮繼承的註釋。(如果沒有直接出現在該元素上,則返回 length 為 0 的陣列。)此方法的呼叫方可以自由修改返回的陣列;這不會對返回給其他呼叫方的陣列產生任何影響。
宣告
以下是 java.lang.reflect.Constructor.getDeclaredAnnotations() 方法的宣告。
public Annotation[] getDeclaredAnnotations()
返回
直接出現在此元素上的所有註釋。
示例
以下示例顯示了 java.lang.reflect.Constructor.getDeclaredAnnotations() 方法的用法。
即時演示package com.tutorialspoint;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
public class ConstructorDemo {
public static void main(String[] args) {
Constructor[] constructors = SampleClass.class.getConstructors();
Annotation[] annotations = constructors[0].getDeclaredAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof CustomAnnotation){
CustomAnnotation customAnnotation = (CustomAnnotation) annotation;
System.out.println("name: " + customAnnotation.name());
System.out.println("value: " + customAnnotation.value());
}
}
}
}
@CustomAnnotation(name = "SampleClass", value = "Sample Class Annotation")
class SampleClass {
private String sampleField;
@CustomAnnotation(name="sampleClassConstructor", value = "Sample Constructor Annotation")
public SampleClass(){
}
public SampleClass(String sampleField){
this.sampleField = sampleField;
}
public String getSampleField() {
return sampleField;
}
public void setSampleField(String sampleField) {
this.sampleField = sampleField;
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
public String name();
public String value();
}
讓我們編譯並執行上述程式,這將產生以下結果 −
name: sampleClassConstructor value: Sample Constructor Annotation
java_reflect_constructor.htm
廣告