【Java】注解与反射(一)——注解

✨内置注解

@Override 重写

@Deprecated 不推荐使用 但是可以使用 或者存在更改好的方法(已废弃)

@SuppreddWarings("all") 抑制警告


✨元注解

@Target 表示注解可以用在哪些地方

@Retention 表示注解在什么地方有效 RUNTIME > CLASS > SOURCE

@Documented 表示是否将注解生成在Javadoc中

@Inherited 子类可以继承父类的注解


✨自定义注解

package com.example.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class Test03 {

//    显式赋值 没有默认值必须赋值
    @MyAnnotation2(name = "name", schools = {"BiliBili"})
    public void test1(){}

    @MyAnnotation3("可以省略value = ")
    public void test2(){}
}

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
//    注解参数:参数类型 + 参数名();
    String name() default "";
    int age() default 0;
//    如果默认值为-1 代表不存在
    int id() default -1;
    String[] schools() default {"school"};
}


@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
    String value();
}

✨课程链接

【狂神说Java】注解和反射_哔哩哔哩_bilibili


⭐转载请注明出处

本文作者:双份浓缩馥芮白

原文链接:https://www.cnblogs.com/Flat-White/p/15157221.html

版权所有,如需转载请注明出处。

原文地址:https://www.cnblogs.com/Flat-White/p/15157221.html