随笔 - 158  文章 - 0 评论 - 4 阅读 - 19万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

场景:为了理解@interface使用
1.@interface自定义注解
<1>@interface自定义注解自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。
<2>在定义注解时,不能继承其他的注解或接口。
<3>使用@interface来声明一个注解,
1>.每一个方法实际上是声明了一个配置参数,
2>.方法的名称就是参数的名称,
3>.返回值类型就是参数的类型,(返回值类型只能是基本类型、Class、String、enum)
4>.可以通过default来声明参数的默认值
2.举例说明,分别作用在类,方法,属性上的注解
<1>.作用在属性上注解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FiledAnnotation {

    String value() default "GetFiledAnnotation";
}

<2>.作用在方法上注解

复制代码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnnotation {

    String name() default "MethodAnnotation";

    String url() default "https://www.cnblogs.com";
}
复制代码

<3>.作用在类上注解

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TypeAnnotation {

    String value() default "TypeAnnotation";
}

<4>.使用自定义注解

复制代码
@TypeAnnotation("doWork")
public class Worker {

    @FiledAnnotation("博客园")
    private String myField = "";

    @MethodAnnotation
    public String getDefaultInfo() {
        return "do the getDefaultInfo method";
    }

    @MethodAnnotation(name = "百度", url = "www.baidu.com")
    public String getDefineInfo() {
        return "do the getDefineInfo method";
    }
}
复制代码

<5>.测试自定义注解

复制代码
public class TestAnnotation {

    public static void main(String[] args) throws ClassNotFoundException {
        Class cls = Class.forName("com.liuqing.annotation.Worker");
        Method[] method = cls.getMethods();
        /**判断Worker类上是否有TypeAnnotation注解*/
        boolean flag = cls.isAnnotationPresent(TypeAnnotation.class);
        /**获取Worker类上是TypeAnnotation注解值*/
        if (flag) {
            TypeAnnotation typeAnno = (TypeAnnotation) cls.getAnnotation(TypeAnnotation.class);
            System.out.println("@TypeAnnotation值:" + typeAnno.value());
        }

        /**方法上注解*/
        List<Method> list = new ArrayList<Method>();
        for (int i = 0; i < method.length; i++) {
            list.add(method[i]);
        }

        for (Method m : list) {
            MethodAnnotation methodAnno = m.getAnnotation(MethodAnnotation.class);
            if (methodAnno == null)
                continue;
            System.out.println("方法名称:" + m.getName());
            System.out.println("方法上注解name = " + methodAnno.name());
            System.out.println("方法上注解url = " + methodAnno.url());
        }
        /**属性上注解*/
        List<Field> fieldList = new ArrayList<Field>();
        for (Field f : cls.getDeclaredFields()) {// 访问所有字段
            FiledAnnotation filedAno = f.getAnnotation(FiledAnnotation.class);
            System.out.println("属性名称:" + f.getName());
            System.out.println("属性注解值FiledAnnotation = " + filedAno.value());
        }
    }
}
复制代码

打印结果

 

posted on   一中晴哥威武  阅读(2564)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示