java注解和反射-- 自定义注解

自定义注解

使用@interface自定义注解时,自动继承了java.lang .annotation.Annotation接口

package cn.usts.edu.SelfDesignAnnotation;

import java.lang.annotation.*;

/**
 * 自定义注解
 *      注解参数的使用
 *      类型+ 参数名() default 默认值
 * */

public class SelfAnnotation {
    
    


    @Self
    public void test01(){
    
    

    }

    @Self(name = "test01", age = 12,sex = "男",id = 15,school = {
    
    "试验附中"})
    public void test02(){
    
    

    }

}


@Target(value = ElementType.METHOD) // 注解生效位置,方法上
@Retention(value = RetentionPolicy.RUNTIME) // 到那个位置还有效果
@interface Self{
    
    
//    类型   参数名   默认值
    String name() default "name1";
    int age() default 18;
    String sex() default "";
    int id() default -1;// 表示不存在
    String[] school() default {
    
    "清华","北大"};

}

猜你喜欢

转载自blog.csdn.net/qq_43619461/article/details/120991218