Java Class类 isAnnotationPresent()方法与示例
Class类isAnnotationPresent()方法
isAnnotationPresent()方法在java.lang包中可用。
当给定类型的注释存在于此实体上时,isAnnotationPresent()方法将返回true,否则将返回false。
isAnnotationPresent()方法是一个非静态方法,只能使用类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
isAnnotationPresent()方法:在检查当前注释时可能会引发异常。
NullPointerException:在此异常中,当给定的注释类为null时。
语法:
public boolean isAnnotationPresent(Class ann_class);
参数:
Classan_class–表示与注释类型相似或对应的Class对象。
返回值:
此方法的返回类型为boolean,它根据以下情况返回布尔值:
当给定类型的注释存在于此实体上时,它返回true。
当给定类型的注释不存在时,它返回false。
示例
//Java程序演示示例
//的booleanisAnnotationPresent(Classan_class)方法的说明
import java.security.*;
public class IsAnnotationPresentOfClass {
public static void main(String[] args) throws Exception {
Class ann1 = Identity.class;
Class ann2 = Deprecated.class;
//我们正在检查已弃用的注释当前类型
//使用方法isAnnotationPresent()
boolean b1 = ann2.isAnnotationPresent(ann2);
System.out.println("is Deprecated an Annotation Present type" + " " + b1);
//我们正在检查Identity类的AnnotationPresent类型
//通过使用方法isAnnotationPresent()
boolean b2 = ann1.isAnnotationPresent(ann1);
System.out.println("is Deprecated an Annotation Present type" + " " + b2);
}
}输出结果
is Deprecated an Annotation Present type false is Deprecated an Annotation Present type false
热门推荐
10 广西考试祝福语结婚简短
11 猪年祝福语简短小孩
12 元旦祝福语送长辈简短
13 恭喜二宝祝福语简短
14 祝福语暖心话简短
15 国庆中秋祝福语简短兄弟
16 朋友订婚的祝福语简短
17 送弟弟中秋祝福语简短
18 爱生日祝福语简短独特