ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

注解的初理解

2022-05-22 14:34:26  阅读:101  来源: 互联网

标签:String System value 理解 println 注解 out


注解

注解是java中给代码注释的,java中自带常用的注解有@Override是表重写父类方法的注解

元注解:java有四个元注解,有了这四个元注解我们就可以自己定义注解了

@Target
/*
描述了注解修饰的对象范围,取值在java.lang.annotation.ElementType定义
METHOD:用于描述方法
PACKAGE:用于描述包
PARAMETER:用于描述方法变量
TYPE:用于描述类、接口或enum类型
*/

@Retention
/*
表示注解保留时间长短。取值在java.lang.annotation.RetentionPolicy定义
RetentionPolicy.SOURCE -------------注解将被编译器丢弃
RetentionPolicy.CLASS -------------注解在class文件中可用,但会被VM丢弃
RetentionPolicy.RUNTIME ---------VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息
RUNTIME > CLASS > SOURCE
自定义注解一般使用RUNTIME
*/

@Documented
/*
Documented注解的作用是:描述在使用 javadoc 工具为类生成帮助文档时是否要保留其注解信息。
*/

@Inherited
/*
Inherited注解的作用是:使被它修饰的注解具有继承性(如果某个类使用了被@Inherited修饰的注解,则其子类将自动具有该注解)
*/

下面是自定义的注解:

//定义个自己的注解
@Target({ElementType.METHOD,ElementType.TYPE})//METHOD可以在方法上使用,TYPE可以在类上使用
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    String name() default "";  //前面(String)是关键字,name()是变量名,default表示默认的值
    int age() default 0;       //如果定了的变量,没有给默认值,使用注解时需要传入值
    int id() default 1;

    String[] schools() default {"重庆交通大学"}; //定义一个数组变量
}

反射获取注解:

public class Test02 {
    public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("TestDemo.annotation.Person");

        System.out.println("获取全部注解:");
        Annotation[] annotations = aClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        System.out.println("=============================================");

        System.out.println("获取指定类上的注解:");
        MyType annotation = aClass.getAnnotation(MyType.class);
        System.out.println(annotation);
        System.out.println(annotation.value());
        System.out.println("=============================================");

        System.out.println("获取指定属性上的注解");
        Field name = aClass.getDeclaredField("name");
        MyField myField = name.getAnnotation(MyField.class);
        System.out.println(myField);
        System.out.println(myField.columnName());
        System.out.println(myField.type());
        System.out.println(myField.length());

    }
}

@MyType("sql_value")
class Person{

    @MyField(columnName = "sql_value",type = "int",length = 11)
    private int id;
    @MyField(columnName = "sql_value",type = "int",length = 11)
    private int age;
    @MyField(columnName = "sql_value",type = "String",length = 20)
    private String name;

}

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

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyField{
    String columnName();
    String type();
    int length();
}

/*
结果:

获取全部注解:
@TestDemo.annotation.MyType(value=sql_value)
=============================================
获取指定类上的注解:
@TestDemo.annotation.MyType(value=sql_value)
sql_value
=============================================
获取指定属性上的注解
@TestDemo.annotation.MyField(columnName=sql_value, type=String, length=20)
sql_value
String
20

*/

标签:String,System,value,理解,println,注解,out
来源: https://www.cnblogs.com/shuisanya/p/16297800.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有