
举例
package com.atguigu.java1;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Date;
public class AnnotationTest1 {
public static void main(String[] args) {
com.atguigu.java1.Person p = new com.atguigu.java1.Student();
p.walk();//学生走路
//使用过时的结构会有一个线,但是不影响使用,还可以用。
Date date = new Date(2020, 10, 11);
System.out.println(date);//Thu Nov 11 00:00:00 CST 3920
@SuppressWarnings("unused")//表示 定义的num没有使用
int num = 10;
// System.out.println(num);
@SuppressWarnings({ "unused", "rawtypes" })// rawtypes是说传参时也要传递带泛型的参数, ArrayList
ArrayList list = new ArrayList();
}
@Test
public void testGetAnnotation(){
Class clazz = com.atguigu.java1.Student.class;
Annotation[] annotations = clazz.getAnnotations();
for(int i = 0;i < annotations.length;i++){
System.out.println(annotations[i]);
}
}
}
class Person{
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void walk(){
System.out.println("人走路");
}
public void eat(){
System.out.println("人吃饭");
}
}
interface Info{
void show();
}
class Student extends Person implements Info {
@Override
public void walk() {
System.out.println("学生走路");
}
@Override//不写不代表下面不是重写的,写上能校验这是不是重写,比如如果写成show1,就校验出来这不是重写了.
public void show() {
}
}
注解和代码
如何自定义注解
jdk中四个元注解的使用
6.2 类型注解:
ElementType.TYPE_PARAMETER 表示该注解能写在类型变量的声明语句中(如:泛型声明)。ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。
加上红框这个词
不再报错
报错
加上这个词
不在报错
测试代码
自定义注解:MyAnnotation
package com.atguigu.java1;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
@Inherited//加上这个注解后 标明被它修饰的 Annotation 将具有继承性。
@Repeatable(MyAnnotations.class)//这样后就能两排可重复注解不报错了
//@MyAnnotation(value="hi")
//@MyAnnotation(value="abc")这样不报错了
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER,TYPE_USE})
//Target表示这个注解可以修饰什么元素,有TYPE该注解就能修饰类、接口,有FIELD就能修饰字段声明,有METHOD就能修饰方法
public @interface MyAnnotation {
String value() default "hello";
}
自定义注解(为重复注解而写的自定义注解):MyAnnotations
package com.atguigu.java1;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
@Inherited
@Retention(RetentionPolicy.RUNTIME)//和MyAnnotation一样的生命周期
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})//和MyAnnotation一样的能修饰的范围
public @interface MyAnnotations {
MyAnnotation[] value();
}
注解测试总类:AnnotationTest
package com.atguigu.java1;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Date;
public class AnnotationTest {
public static void main(String[] args) {
Person p = new Student();
p.walk();
//使用过时的结构会有一个线,但是不影响使用,还可以用。
Date date = new Date(2020, 10, 11);
System.out.println(date);
@SuppressWarnings("unused")//表示 定义的num没有使用
int num = 10;
// System.out.println(num);
@SuppressWarnings({ "unused", "rawtypes" })// rawtypes是说传参时也要传递带泛型的参数, ArrayList
ArrayList list = new ArrayList();
}
@Test//这段代码测试的是Inherited元注解的继承性,这里暂且当一个@Test的举例来看
public void testGetAnnotation(){
Class clazz = Student.class;
Annotation[] annotations = clazz.getAnnotations();
for(int i = 0;i < annotations.length;i++){
System.out.println(annotations[i]);
}
}
}
//重复注解↓
//jdk 8之前的写法:
//@MyAnnotations({@MyAnnotation(value="hi"),@MyAnnotation(value="hi")})
@MyAnnotation(value="hi")
@MyAnnotation(value="abc")
class Person{
private String name;
private int age;
public Person() {
}
@MyAnnotation
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@MyAnnotation
public void walk(){
System.out.println("人走路");
}
public void eat(){
System.out.println("人吃饭");
}
}
interface Info{
void show();
}
class Student extends Person implements Info{
@Override
public void walk() {
System.out.println("学生走路");
}
@Override//不写不代表下面不是重写的,写上能校验这是不是重写,比如如果写成show1,就校验出来这不是重写了.
public void show() {
}
}
class Generic<@MyAnnotation T>{
public void show() throws @MyAnnotation RuntimeException{
ArrayList<@MyAnnotation String> list = new ArrayList<>();
int num = (@MyAnnotation int) 10L;
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)