java.lang.reflect.Constructor.getParameterAnnotations()方法示例

java.lang.reflect.Constructor.getParameterAnnotations()方法返回一個數組的數組,它們表示由Constructor對象表示的方法的形式參數(以聲明順序)的注釋。 (如果底層方法是無參數的,則返回一個長度為零的數組,如果該方法具有一個或多個參數,則對於每個沒有注釋的參數,返回長度為零的嵌套數組)。返回的數組中包含的注釋對象是可序列化的。 該方法的調用者可以自由修改返回的數組; 它將對返回給其他調用者的數組沒有影響。

聲明

以下是java.lang.reflect.Constructor.getParameterAnnotations()方法的聲明。

public Annotation[][] getParameterAnnotations()

返回值

  • 底層聲明的數組。

例子

以下示例顯示了java.lang.reflect.Constructor.getParameterAnnotations()方法的用法。


import java.lang.reflect.Constructor;

public class GetName {

   public static void main(String[] args) {
      Constructor[] constructors = SampleClass.class.getDeclaredConstructors();
      for (Constructor constructor : constructors) {
         System.out.println("Constructor: " + constructor.getName());
      }
   }
}

class SampleClass {
   private String sampleField;

   public SampleClass() throws ArrayIndexOutOfBoundsException {
   }

   private SampleClass(String sampleField){
      this.sampleField = sampleField;
   }

   public String getSampleField() {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField;
   }
}

讓我們編譯並運行上面的程式,這將產生以下結果 -

F:\worksp\javareflect>java GetParameterAnnotations
name: sampleClassConstructor
value: Sample Constructor Annotation

上一篇: java.lang.reflect Constructor<T>類 下一篇: java.lang.reflect.Field類