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 送给学长毕业祝福语简短