文章目录
- 注解
 - 内置注解
 - **@Deprecated**
 - @Override
 - @SuppressWarnings【不建议使用】
 - @Funcationallnterface
 
- 自定义注解
 - 元注解
 - @Retention
 - @Target
 - @Documented
 - @Inherited 和 @ Repeatable
 
- 反射注解
 
前言:笔记基于动力节点
注解
- 注解可以标注在 类上,属性上,方法上等。
 - 注解可以做到在不改变代码逻辑的前提下在代码中嵌入补充信息
 
注解:给编译器看的,或给其他程序看的,程序根据有没有这个注解来决定不同的处理方式
框架 = 反射 + 注解 + 设计模式
内置注解

@Deprecated
/*
 * JDK 的内置注解:@Deprecated
 * 1. 被这个注解标注的元素已过时
 * 2. 这个注解是给编译器看的,编译器看到这个注解之后会有警告提示信息
 * 3. 可以标注属性,方法,类
 */
public class Test {
    //这里有警告
    MyClass1 myClass1 = new MyClass1();
}
@Deprecated
class MyClass1 {
    // since 属性表示从哪个版本开始已过时
    // forRemoval 属性值如果是 true 表示已移除
    @Deprecated(since = "9", forRemoval = true)
    public static int num = 100;
    @Deprecated
    public void doSoms() {
    }
}
 

@Override
/*
 * JDK 的内置注解:@Override
 * 1. 给编译器看的
 * 2. 这个注解标注实例方法,被标注的方法必须是重写父类的方法
 * 3. 这个注解就是在编译阶段进行方法检查,检查这个方法是否重写了父类,如果没有重写父类报错
 * 4. 只能标注实例方法
 */
public class Test extends myclass{
    @Override
    void test() {
        
    }
}
class myclass {
    
    void test() {};
}
 
@SuppressWarnings【不建议使用】
/*
 * JDK 的内置注解:@SuppressWarnings
 * 1. 主要作用:抑制警告
 * 2. 该注解常见的属性值
 *     rewtypes: 抑制未使用的泛型的警告
 *     resource: 抑制未关闭资源的警告
 *     deprecation: 抑制使用了已过时资源的警告
 *     all: 抑制所有警告
 */
public class Test {
    public static void main(String[] args) throws FileNotFoundException {
        //取消没用泛型的警告
        @SuppressWarnings("rawtypes")
        List list = new ArrayList();
        //取消没关流的警告
        @SuppressWarnings("resource")
        FileInputStream in = new FileInputStream("e:\\file.txt");
    }
}
 
@Funcationallnterface
检查是否符合 “函数式接口” 的特征
/*
 * JDK 的内置注解:@Functionallnterface
 * 1. 这个注解专门用来标注接口的
 * 2. 被标记的接口必须是一个函数式接口,如果不是函数式接口,编译器报错
 * 3. 这个注解也是给编译器看的
 * 4. 函数式接口:接口中抽象方法有且仅有一个
*/
public class Test {
    public static void main(String[] args) throws FileNotFoundException {
        
    }
}
@FunctionalInterface
interface T {
    void fly();
}
 
自定义注解
使用 @interface 来定义注解
默认情况下可以出现在类,方法,属性,构造方法,方法参数上
所有自定义注解,父类是:java.lang.annotation.Annotation

public @interface MyAnnotation {
    /**
     * 注解也可以定义属性,属性名后面必须添加 ()
     */
    //属性
    String drive();
    String url();
    String[] ab();
}
 
public class Test {
    public static void main(String[] args){
		//使用注解
        @MyAnnotation(
                drive = "aaa",
                url = "abc",
                ab = {"1,2,3"},
        )
        int i = 10;
    }
}
 
元注解

@Retention

@Retention(value = RetentionPolicy.SOURCE)
@Retention(value = RetentionPolicy.CLASS)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
}
 
@Target

@Documented

被这个注解声明的注解,可以生成帮助文档
@Inherited 和 @ Repeatable

反射注解

- field.isAnnotationPersent(AnnotationTest02.class):判断有没有这个注解
 - AnnotationTest02 an = field.getAnnotation(AnnotationTest02.class):获取注解
 







![【解决ERROR】usage:conda [-h][-V] command... conda:error:unrecognized arguments](https://i-blog.csdnimg.cn/direct/910327c89610460da5a6129b207a0925.png)











