當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Java.lang.Class.getAnnotations()用法及代碼示例


描述

這個java.lang.Class.getAnnotations() 方法返回此元素上存在的所有注釋。如果此元素沒有注釋,則返回長度為零的數組。該方法的調用者可以自由修改返回的數組;它不會影響返回給其他調用者的數組。

聲明

以下是聲明java.lang.Class.getAnnotations()方法

public Annotation[] getAnnotations()

參數

NA

返回值

此方法返回此元素上存在的所有注釋。

異常

NA

示例

下麵的例子展示了 java.lang.Class.getAnnotations() 方法的用法。

package com.tutorialspoint;

import java.lang.annotation.Annotation;
   
public class ClassDemo {

   public static void main(String []args) {

      ClassDemo cls = new ClassDemo();
      Class c = cls.getClass();

      Annotation[] a = c.getAnnotations();
      if(a.length != 0) {
         for(Annotation val:a) {
            System.out.println(val.toString());
         }
      } else {
         System.out.println("Annotations is not present...");
      }
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果——

Annotations is not present...

相關用法


注:本文由純淨天空篩選整理自 Java.lang.Class.getAnnotations() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。