关于注解首先引入官方文档的一句话:Java 注解用于为 Java 代码提供元数据。做为元数据,注解不直接影响你的代码执行,但也有一些类型的注解实际上能够用于这一目的。Java 注解是从 Java5 开始添加到 Java 的。看完这句话也许你仍是一脸懵逼,接下我将从注解的定义、元注解、注解属性、自定义注解、注解解析JDK 提供的注解这几个方面再次了解注解(Annotation)git
public @interface MyTestAnnotation {
}
复制代码
@MyTestAnnotation
public class test {
@MyTestAnnotation
public static void main(String[] args){
}
}
复制代码
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTestAnnotation {
}
复制代码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTestAnnotation {
}
复制代码
/**自定义注解*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTestAnnotation {
}
/**父类标注自定义注解*/
@MyTestAnnotation
public class Father {
}
/**子类*/
public class Son extends Father {
}
/**测试子类获取父类自定义注解*/
public class test {
public static void main(String[] args){
//获取Son的class对象
Class<Son> sonClass = Son.class;
// 获取Son类上的注解MyTestAnnotation能够执行成功
MyTestAnnotation annotation = sonClass.getAnnotation(MyTestAnnotation.class);
}
}
复制代码
/**一我的喜欢玩游戏,他喜欢玩英雄联盟,绝地求生,极品飞车,尘埃4等,则咱们须要定义一我的的注解,他属性表明喜欢玩游戏集合,一个游戏注解,游戏属性表明游戏名称*/
/**玩家注解*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface People {
Game[] value() ;
}
/**游戏注解*/
@Repeatable(People.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Game {
String value() default "";
}
/**玩游戏类*/
@Game(value = "LOL")
@Game(value = "PUBG")
@Game(value = "NFS")
@Game(value = "Dirt4")
public class PlayGame {
}
复制代码
/**注解Repeatable源码*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Repeatable {
/**
* Indicates the <em>containing annotation type</em> for the
* repeatable annotation type.
* @return the containing annotation type
*/
Class<? extends Annotation> value();
}
复制代码
/**Annotation接口源码*/
public interface Annotation {
boolean equals(Object obj);
int hashCode();
Class<? extends Annotation> annotationType();
}
复制代码
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTestAnnotation {
String name() default "mao";
int age() default 18;
}
@MyTestAnnotation(name = "father",age = 50)
public class Father {
}
复制代码
/**是否存在对应 Annotation 对象*/
public boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return GenericDeclaration.super.isAnnotationPresent(annotationClass);
}
/**获取 Annotation 对象*/
public <A extends Annotation> A getAnnotation(Class<A> annotationClass) {
Objects.requireNonNull(annotationClass);
return (A) annotationData().annotations.get(annotationClass);
}
/**获取全部 Annotation 对象数组*/
public Annotation[] getAnnotations() {
return AnnotationParser.toArray(annotationData().annotations);
}
复制代码
public class test {
public static void main(String[] args) throws NoSuchMethodException {
/**
* 获取类注解属性
*/
Class<Father> fatherClass = Father.class;
boolean annotationPresent = fatherClass.isAnnotationPresent(MyTestAnnotation.class);
if(annotationPresent){
MyTestAnnotation annotation = fatherClass.getAnnotation(MyTestAnnotation.class);
System.out.println(annotation.name());
System.out.println(annotation.age());
}
/**
* 获取方法注解属性
*/
try {
Field age = fatherClass.getDeclaredField("age");
boolean annotationPresent1 = age.isAnnotationPresent(Age.class);
if(annotationPresent1){
Age annotation = age.getAnnotation(Age.class);
System.out.println(annotation.value());
}
Method play = PlayGame.class.getDeclaredMethod("play");
if (play!=null){
People annotation2 = play.getAnnotation(People.class);
Game[] value = annotation2.value();
for (Game game : value) {
System.out.println(game.value());
}
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
}
复制代码
运行结果:github
注解 | 做用 | 注意事项 |
---|---|---|
@Override | 它是用来描述当前方法是一个重写的方法,在编译阶段对方法进行检查 | jdk1.5中它只能描述继承中的重写,jdk1.6中它能够描述接口实现的重写,也能描述类的继承的重写 |
@Deprecated | 它是用于描述当前方法是一个过期的方法 | 无 |
@SuppressWarnings | 对程序中的警告去除。 | 无 |
Java 注解用于为 Java 代码提供元数据。做为元数据,注解不直接影响你的代码执行,但也有一些类型的注解实际上能够用于这一目的。数组
/**定义限额注解*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface BankTransferMoney {
double maxMoney() default 10000;
}
/**转帐处理业务类*/
public class BankService {
/**
* @param money 转帐金额
*/
@BankTransferMoney(maxMoney = 15000)
public static void TransferMoney(double money){
System.out.println(processAnnotationMoney(money));
}
private static String processAnnotationMoney(double money) {
try {
Method transferMoney = BankService.class.getDeclaredMethod("TransferMoney",double.class);
boolean annotationPresent = transferMoney.isAnnotationPresent(BankTransferMoney.class);
if(annotationPresent){
BankTransferMoney annotation = transferMoney.getAnnotation(BankTransferMoney.class);
double l = annotation.maxMoney();
if(money>l){
return "转帐金额大于限额,转帐失败";
}else {
return"转帐金额为:"+money+",转帐成功";
}
}
} catch ( NoSuchMethodException e) {
e.printStackTrace();
}
return "转帐处理失败";
}
public static void main(String[] args){
TransferMoney(10000);
}
}
复制代码
运行结果:bash
到此,对于Java中注解的解析就结束了。最后,也很是感谢您阅读个人文章,文章中若是有错误,请你们给我提出来,你们一块儿学习进步,若是以为个人文章给予你帮助,也请给我一个喜欢和关注,同时也欢迎访问个人我的博客。框架