网站建设的目标是什么?提供了哪些栏目?2023年7 8月十大新闻
文章目录
- 1.概述
- 2.JDK内置注解
- 2.1override注解
- 2.2 Deprecated注解
- 3.元注解
- 4.注解中定义属性
- 4.1 属性value
- 4.2 属性是一个数组
- 5. 反射注解
- 6.注解在开发中的作用
1.概述
注解,也叫注释,是一种引用数据类型。编译后也同样生成class字节码文件。
- 语法
[修饰符列表] @inteface 注解类型名 {
}
- 使用
@注解类型名
可以出现在类上,属性上,方法上,变量上,注解类型上。
package com.sdnu.java.annotation;public @interface MyAnnotation {
}
package com.sdnu.java.annotation;@MyAnnotation
public @interface OtherAnnotation {
}
package com.sdnu.java.annotation;/*** 注解1** @author Beyong* @date 2023/03/02 11:42**/
@MyAnnotation
public class AnnotationTest01 {@MyAnnotationprivate int no;@MyAnnotationpublic AnnotationTest01(){}@MyAnnotationpublic static void m1(){}@MyAnnotationpublic void m2(){}
}@MyAnnotation
interface MyInterface{}@MyAnnotation
enum Season{SPRING,SUMMER,AUTUMN,WINTER
}
2.JDK内置注解
2.1override注解
- 只能注解方法
进行编译检查,如果没有重写父类方法,则会编译器报错。
2.2 Deprecated注解
用来标注已过时。
package com.sdnu.java.annotation;/*** 注解Deprecated** @author Beyong* @date 2023/03/02 20:11**/
public class AnnotationTest03 {public static void main(String[] args) {Test.myMethod();}
}class Test{@Deprecatedpublic static void myMethod(){System.out.println("hhh");}
}
3.元注解
用来标注注解类型的注解,叫做元注解。常见元注解
- Target
用来标记被注解可以出现在哪些位置上。 - Retention
4.注解中定义属性
package com.sdnu.java.annotation;@MyAnnotation
public @interface OtherAnnotation {String name();String color();int age() default 20;
}
4.1 属性value
如果一个注解的属性名是value,则改属性名可以省略。
4.2 属性是一个数组
5. 反射注解
注解
package com.sdnu.java.annotation.annotation5;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {String value() default "北京";
}
package com.sdnu.java.annotation.annotation5;/*** test** @author Beyong* @date 2023/03/02 21:24**/
@MyAnnotation
public class MyAnnotationClassTest {int i;public MyAnnotationClassTest(){}@MyAnnotationpublic void doSome(){int i;}
}
package com.sdnu.java.annotation.annotation5;/*** 反射注解** @author Beyong* @date 2023/03/02 21:27**/
public class ReflectAnnotationTest{public static void main(String[] args) throws Exception{Class c = Class.forName("com.sdnu.java.annotation.annotation5.MyAnnotationClassTest");System.out.println(c.isAnnotationPresent(MyAnnotation.class));if(c.isAnnotationPresent(MyAnnotation.class)){MyAnnotation myAnnotation = (MyAnnotation) c.getAnnotation(MyAnnotation.class);System.out.println("类上的注解对象"+ myAnnotation);String value = myAnnotation.value();System.out.println(value);}Class stringClass = Class.forName("java.lang.String");System.out.println(stringClass.isAnnotationPresent(MyAnnotation.class)); //false}
}
6.注解在开发中的作用
Id类注解
package com.sdnu.java.annotation.annotation7;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Id {
}
User类
package com.sdnu.java.annotation.annotation7;/*** user** @author Beyong* @date 2023/03/02 20:48**/
@Id
public class User {int id;String name;String password;
}
异常类
package com.sdnu.java.annotation.annotation7;/*** 自定义异常** @author Beyong* @date 2023/03/02 20:55**/
public class HasNotIdPropertyException extends RuntimeException {public HasNotIdPropertyException(){}public HasNotIdPropertyException(String s){super(s);}
}
测试类
package com.sdnu.java.annotation.annotation7;import java.lang.reflect.Field;/*** test** @author Beyong* @date 2023/03/02 20:48**/
public class Test {public static void main(String[] args) throws Exception{Class userClass = Class.forName("com.sdnu.java.annotation.annotation7.User");boolean isOk = false;if (userClass.isAnnotationPresent(Id.class)){Field[] fields = userClass.getDeclaredFields();for(Field field : fields){if("id".equals(field.getName()) && "int".equals(field.getType().getSimpleName())){isOk = true;break;}}if(!isOk){throw new HasNotIdPropertyException("被标准的类必须有一个int类型的id属性");}}}
}
不改变上述代码:
改变User类中的int id 为其它,则出现:
作者:Beyong
出处:Beyong博客
github地址:https://github.com/beyong2019
本博客中未标明转载的文章归作者Beyong有,欢迎转载,但未经作者同意必须保留此段声明,且在文章明显位置给出原文连接,否则保留追究法律责任的权利。