java注解

常用注解

@Override 继承父类
@Deprecated 方法已经过时 @SupperessWarnings(“depresscated) 忽略警告

注解分类

1、源码注解 只存在于源码中,编译成.class文件就不存在了。
2、编译时注解 在源码和.class 文件中存在的注解
3、运行时注解 在运行阶段还起作用,甚至会影响运行逻辑。

语法

@Target({ElementType.METHOD,ElementType.TYPE})
@Retantion(RetentionPolicy>RUNTIME)
@Inherited
@Documented
  public @interface Description{

    String desc();

    String author();

    int age() default 18;
}

1、成员类型是受限的,合法的类型包括原始类型及StringClass ,Annotation ,Enumeration
2、如果只有一个成员的时候,成员名必须取名为value(),在使用时可以忽略成员名和赋值号(=)
3、注解类可以没有成员,没有成员的注解叫做标示注解

元注解

@Target 注解作用域

            constructor     构造方法声明
            file        文件声明
            local variable      局部变量声明
            method      方法申明
            package     包申明
            parameter   参数申明
            type        类接口

@Retention 生命周期

            source  只在源码中显示,编译时丢弃
            class       编译时会记录到class中,运行时忽略
            runtime 运行时存在,可通过反射获取

@Inherited 允许子类继承
@Docunmented 生成javadoc时包含注解的信息

自定义注解的使用

@<注解名>(<成员名1>=<成员值1>,<成员名2>=<成员值2>,……)
@Description(desc=“I am eyeColor”, author=“Mooc boy” ,age=18)
public String eyeColor(){
return “red”;
}


Test

@Table("pzh")
public class User {
    @Column("name1")
    private String name;
    @Column("nickName1")
    private String nickName;
    @Column("age1")
    private int age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getNickName() {
        return nickName;
    }
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        User user = new User();
        user.setName("zjp");
        show(user);
    }

    private static void show(User user) {
        Class class1 = user.getClass();
        boolean isExit = class1.isAnnotationPresent(Table.class);
        if (isExit) {
            Table table = (Table) class1.getAnnotation(Table.class);
            System.out.println("table value=" + table.value());
        }
        Field[] files = class1.getDeclaredFields();
        for (Field field : files) {
            if (field.isAnnotationPresent(Column.class)) {
                Column column = field.getAnnotation(Column.class);
                System.out.println("column value=" + column.value());
                System.out.println("file name=" + field.getName());
                String filedName=field.getName();
                String methodNme="get"+filedName.substring(0, 1).toUpperCase()+filedName.substring(1);
                try {
                    Method method=class1.getMethod(methodNme);
                    Object fileValue=method.invoke(user,null);
                    System.out.println("file value=" + fileValue);

                }  catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }

    }

}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值