Java注解怎么自定义和使用

发布时间:2022-02-23 16:14:44 作者:iii
来源:亿速云 阅读:123

这篇文章主要介绍了Java注解怎么自定义和使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Java注解怎么自定义和使用文章都会有所收获,下面我们一起来看看吧。

一、注解的概念

1、注解官方解释

注解

叫元数据,一种代码级别的说明,它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举在同一个层次,它可以声明在包、类、字段、局部变量、方法参数等的前面,用来对这些元素进行说明、注释。

注解的作用分类

注解按照运行机制分类

2、注解与注释的区别

(1)注解:用于描述代码,说明程序,主要目的是为了给计算机看,且能够影响程序的运行。

(2)注释:用于描述代码的作用和一些关键性的知识点,使用文字描述程序,是为了给程序员观看,以此来使程序员能够以最快的时间了解被注释的代码。


二、内置注解与元注解

1、常用的内置注解

2、常用的元注解

元注解:用于描述注解的注解,在创建注解时使用

1. @Target属性值:

2.@Retention属性值:

3.@Documented:该注解的作用就是表示此注解标记的注解可以包含到javadoc文件中去
4.@Inherited:描述注解是否能够被子类所继承

三、自定义注解

1、自定义注解基础知识

1.格式:

@Inherited//元注解public @interface zhujie{}

2.注解本质:注解的本质上就是一个接口,该接口默认继承Annotation

public interface MyAnno extends java.lang.annotation.Annotion

3.属性:接口中可以定义的内容(成员方法、抽象方法)

属性的返回值:

属性赋值注意事项


2、演示自定义注解的使用

自定义注解annotation

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.TYPE)
public @interface annotation {
    String name() default "木鱼";
    int age();
    int[] score();
}

使用以上注解的类TestAnnotation

//name具有默认值,不需要必须为name赋值,但也可以重新赋值
@annotation(age=20,score={99,100,100})
public class TestAnnotation {

    public static void main(String[] args) throws ClassNotFoundException {
        Class clazz = Class.forName("test.TestAnnotation");
        annotation annotation = (annotation) clazz.getAnnotation(annotation.class);
        System.out.println("姓名:"+annotation.name()+"  年龄:"+annotation.age());
        System.out.print("成绩为:");
        int[] score=annotation.score();
        for (int score1:score){
            System.out.print(score1+" ");
        }
    }

}

3、演示注解在程序中的作用

两个方法:

1.创建自定义注解

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.FIELD)
public @interface StringNull {

}

2.创建实体类

public class Student {
    @StringNull
    public String name=null;

    @StringNull
    public String xuehao=null;

    @StringNull
    public String sex=null;

    public void setName(String name) {
        this.name = name;
    }

    public void setXuehao(String xuehao) {
        this.xuehao = xuehao;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

3.创建测试类,测试注解

public class TestAnnotation {

    public static void main(String[] args) throws Exception{
        Class clazz = Class.forName("test.Student");
        Student student =(Student) clazz.newInstance();
        student.setName("小明");
        Field[] fields= clazz.getFields();
        for(Field f:fields){
            if(f.isAnnotationPresent(StringNull.class)){
                if(f.get(student)==null){
                    System.out.println(f.getName()+":是空的字符串属性");
                }else{
                    System.out.println(f.getName()+":"+f.get(student));
                }
            }
        }
    }
}

关于“Java注解怎么自定义和使用”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Java注解怎么自定义和使用”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Java注解(闻过)
  2. java注解的使用方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:Java怎么实现在指定范围内生成随机整数

下一篇:java中如何接收键盘中的字符

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》