侧边栏壁纸
博主头像
高大北博主等级

所有的再见中,我最喜欢明天见

  • 累计撰写 208 篇文章
  • 累计创建 151 个标签
  • 累计收到 20 条评论
标签搜索

目 录CONTENT

文章目录

反射操作注解

高大北
2022-05-05 / 0 评论 / 1 点赞 / 278 阅读 / 1,518 字 / 正在检测是否收录...

示?

public class Test12 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException {
        Class<?> aClass = Class.forName("com.fl.reflection.Student2");
        //通过反射获得注解
        Annotation[] annotations = aClass.getAnnotations();
        for (Annotation annotation:annotations) {
            System.out.println(annotation);
        }
        //获取注解value的值
        Tablell annotation = (Tablell) aClass.getAnnotation(Tablell.class); //获取指定注解
        System.out.println(annotation.value());
        //获得类指定的 注解
        Field name = aClass.getDeclaredField("name");
        Fieldll annotation1 = name.getAnnotation(Fieldll.class);
        System.out.println(annotation1.culumeName());
        System.out.println(annotation1.type());
        System.out.println(annotation1.length());
    }
}
@Tablell("db_student")
class Student2{
    @Fieldll(culumeName="db_id",type = "int",length = 10)
    private int id;
    @Fieldll(culumeName="db_age",type = "int",length = 10)
    private int age;
    @Fieldll(culumeName="db_name",type = "varchar",length = 10)
    private String name;

    public Student2(){

    }

    public Student2(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;
    }
}

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

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

评论区