1.什么是注解

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6ea6b491-546b-4df9-8518-b03e7df8b8f1/IMG_0247.jpg

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/a41aafc0-bc71-4a2a-b06d-994eece0f535/IMG_0248.jpg

package top.ltyzqhh.Annotation;

import java.util.ArrayList;
import java.util.List;

//什么是注解
public class Test01 extends Object{
    //@Override 重写注解
    @Override
    public String toString() {
        return super.toString();
    }

    //@Deprecated 不推荐程序员使用,但是可以使用,或者存在更好的方式
    @Deprecated
    public static void test(){
        System.out.println("Deprecated");
    }

    //SuppressWarnings 镇压警告
    @SuppressWarnings("all")
    public void test02(){
        List list=new ArrayList();
    }

    public static void main(String[] args) {
        test();
    }

}

2.元注解

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/9557716f-720d-4354-97bf-e47d44d7eca8/IMG_0249.jpg

package top.ltyzqhh.Annotation;

import java.lang.annotation.*;

//测试元注解

public class Test03 {
    @MyAnnotation
    public void test(){

    }
}

//定义一个注解
//表是我们的注解可以用在哪些地方
@Target(value = {ElementType.METHOD,ElementType.TYPE})

//Retention 表示我们的注解在什么地方有效,
//runtime>class>sources
@Retention(value = RetentionPolicy.RUNTIME)

//Documented 表示是否将我们的注解生成在Javadoc中
@Documented

//Inherited 表示子类可以继承父类的注解
@Inherited
@interface  MyAnnotation{

}