ICode9

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

认识注解

2021-02-24 20:29:53  阅读:98  来源: 互联网

标签:name 认识 age int 注解 public String


一.认识注解

1.1 什么是注解?

​ Annotation是jdk1.5开始引入的新技术,可以对程序作出解释,方便被其他程序(编辑器)读取。

1.2 Annotationg格式

格式:@注释名 (参数)

注解的参数:参数类型 + 参数名()

参数案例:

int age();
String name() default '''';
int id() default 0;

注释案例:

@Annotationg01(age=12)
1.3、内置注解
  • @Override:定义在java.lang.Override中,表示一个方法声明打算重写超类中的另一个方法声明.

  • @Deprecated:定义在java.lang.Deprecated中,此注释可以用于修辞方法,属性,类,表示不鼓励程序员使用这样的元素,通常是因为它很危险或者存在更好的选择.

  • @SuppressWarnings:定义在java.lang.SuppressWarnings中,用来抑制编译时的警告信息.

1.4、元注解

元注解的作用就是负责注解其他注解,Java定义了4个标准的meta-annotation类型。

  • @Target:用于描述注解的使用范围

  • @Retention:表示需要在什么级别保存该注释信息,用于描述注释的生命周期(SOURCE<CLASS<RUNTIME)

  • @Decument:说明该注释将被包含在javadoc中

  • @Inherited:说明子类可以继承父类中的该注释

1.5 自定义注解

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

@interface用来声明一个注解,**格式:public @interface 注解名{定义内容},**在类里面定义省略public

/自定义注解
public class Annotation01 {
    //注解可以显示赋值,如果没有默认值,就必须给注解赋值
    @Annotation2(schools = {"清华大学","北京大学"})
    public void test(){}
}
// 自定义一个注解
//Target 表示注解可以用在哪个地方
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface Annotation2{
    //注解的参数:参数类型 + 参数名 ();
    String name() default "";
    int age() default 0;
    String[] schools() default "";
}

二.反射操作注解

2.1获取注解的方法
  • Class.getAnnotations() 获取所有的注解,包括自己声明以及继承的
  • Class.getAnnotation(Class< A > annotationClass) 获取指定的注解
  • Class.getDeclaredAnnotations() 获取自己声明的注解
//反射操作注解
public class Annotation02 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class s1 = Class.forName("com.sun.pojo.Student02");
        //通过反射获得注解
        Annotation[] annotations = s1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation);
        }
        //获得注解value的值
        Tablesun annotation = (Tablesun)s1.getAnnotation(Tablesun.class);
        String value = annotation.value();
        System.out.println(value);

        //获得类指定的注解
        Field name = s1.getDeclaredField("name");
        Filedsun annotation1 = name.getAnnotation(Filedsun.class);
        System.out.println(annotation1.columnName());
        System.out.println(annotation1.type());
        System.out.println(annotation1.length());


    }
}
@Tablesun("db_student")
class Student02{
    @Filedsun(columnName="db_id",type = "int",length = 10)
    private int id;
    @Filedsun(columnName="db_age",type = "int",length = 10)
    private int age;
    @Filedsun(columnName="db_name",type = "varchar",length = 5)
    private String name;

    public Student02() {
    }

    public Student02(int id, int age, String name) {
        this.id = id;
        this.age = age;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "Student02{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Tablesun{
    String value();
}

//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface Filedsun{
    String columnName();
    String type();
    int length();
}

运行结果:

在这里插入图片描述

反射知识总结:https://blog.csdn.net/an760998254/article/details/114004063?spm=1001.2014.3001.5501

标签:name,认识,age,int,注解,public,String
来源: https://blog.csdn.net/an760998254/article/details/114039306

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

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

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

ICode9版权所有