首页 > 编程语言 >Java: Annotaion

Java: Annotaion

时间:2022-11-01 22:02:46浏览次数:41  
标签:java String type field Java Annotaion public name

 

/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 17.01
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DBField.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */



package CoreJava.twelfth;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
 *
 *
 * */
@Documented
@Target(ElementType.FIELD)
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface DBField {
    /**
     *
     *
     * */
    String name();
    /**
     *
     *
     * */
    Class< ?> type();
    /**
     *
     *
     * */
    boolean isPrimaryKey() default false;


}

  

/**
 * 版权所有 2022 涂聚文有限公司
 * 许可信息查看:
 * 描述:
 *
 * 历史版本: JDK 17.01
 * 2022-09-12 创建者 geovindu
 * 2022-09-12 添加 Lambda
 * 2022-09-12 修改:date
 * 接口类
 * 2022-09-12 修改者:Geovin Du
 * 生成API帮助文档的指令:
 *javadoc - -encoding Utf-8 -d apidoc DuUser.java
 * Interface
 * Record
 * Annotation
 * Enum
 * */


package CoreJava.twelfth;
import java.util.Date;

/**
 *
 *
 * */
public class DuUser {
    /**
     *
     *
     * */
    @DBField(name = "id", isPrimaryKey = true, type = Long.class)
    private long id;
    /**
     *
     *
     * */
    @DBField(name = "name", type = String.class)
    private String name;
    /**
     *
     *
     * */
    @DBField(name = "email", type = String.class)
    private String email;
    /**
     *
     *
     * */
    @DBField(name = "createdtime", type = Date.class)
    private Date created;
    /**
     *
     *
     * */
    public long getId() {
        return id;
    }
    /**
     *
     *
     * */
    public void setId(long id) {
        this.id = id;
    }
    /**
     *
     *
     * */
    public String getName() {
        return name;
    }
    /**
     *
     *
     * */
    public void setName(String name) {
        this.name = name;
    }
    /**
     *
     *
     * */
    public String getEmail() {
        return email;
    }
    /**
     *
     *
     * */
    public void setEmail(String email) {
        this.email = email;
    }
    /**
     *
     *
     * */
    public Date getCreated() {
        return created;
    }
    /**
     *
     *
     * */
    public void setCreated(Date created) {
        this.created = created;
    }

}

  

调用:

  System.out.println("Java Custom Annotation Example");
            System.out.println();

            DuUser usr = new DuUser();
            usr.setEmail("[email protected]");
            usr.setName("Geovin Du");
            usr.setId(112);
            usr.setCreated(new Date());

            for (Field field : usr.getClass().getDeclaredFields()) {
                DBField dbField = field.getAnnotation(DBField.class);
                System.out.println("field name: " + dbField.name());

                // changed the access to public
                field.setAccessible(true);
                Object value = field.get(usr);
                System.out.println("field value: " + value);

                System.out.println("field type: " + dbField.type());
                System.out.println("is primary: " + dbField.isPrimaryKey());
                System.out.println();
            }

  

输出:

Java Custom Annotation Example

field name: id
field value: 112
field type: class java.lang.Long
is primary: true

field name: name
field value: Geovin Du
field type: class java.lang.String
is primary: false

field name: email
field value: [email protected]
field type: class java.lang.String
is primary: false

field name: createdtime
field value: Tue Nov 01 21:46:59 CST 2022
field type: class java.util.Date
is primary: false

  

标签:java,String,type,field,Java,Annotaion,public,name
From: https://www.cnblogs.com/geovindu/p/16849311.html

相关文章

  • Javascript笔记 - 数组常用方法
    数组目录数组1.数组基础2.常用数组方法输出:toString()增删:push()、pop()、shift()、unshift()提取:splice()、concat()、slice()3.数组排序sort()reverse()4.数组迭代......
  • JAVA-Integer_int和string的相互转换
    packagecom.itheima;publicclassintger_02{publicstaticvoidmain(String[]args){//int和string的相互转换intnumber=100;//......
  • Javascript笔记 - JS中的函数
    函数目录函数1.函数的声明与定义2.方法3.作用域全局作用域函数作用域声明提前4.构造函数instanceof5.call()和apply()6.this和argumentsthisarguments1.函数的......
  • JAVA-Integer_构造方法
    packagecom.itheima;publicclassintger_01{publicstaticvoidmain(String[]args){Integeri1=newInteger(100);//根据int做创建Integer对象(过时......
  • JavaScript笔记 - JS和html代码的结合方式
    JavaScript和html代码的结合方式方式一在head标签或body标签中,使用script标签来书写JavaScript代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF......
  • java中的抽象类abstract
    /***学习抽象类和抽象方法*抽象方法:只进行方法的声明,而不提供具体的实现(没有方法体)*抽象类:只要在一个类中有一个抽象方法,那么这个就得声明一个抽象类*抽象类和......
  • Javascript笔记 - JS中的变量
    变量目录变量1.变量基础2.强制类型转换3.关系运算1.变量基础JS是一门弱类型语言,这意味变量的类型不是固定的,变量可以随时从一种类型转换为另一种类型vari=1;......
  • javascript
    javascript快速入门内部标签<script>alert("helloword!");</script>外部引入<scriptsrc=""></script>基本语法入门数据类型数据、文本、图形、音......
  • Javascript笔记 - JS中的对象
    对象目录对象1.对象的声明与定义2.原型对象3.枚举对象中属性特殊属性值in运算符hasOwnProperty方法for...in语句1.对象的声明与定义显式声明对象并逐个定义属性......
  • javaSE基础-日期时间
    日期时间类日期时间主要类jdk8之前常用的日期时间APISystem静态方法//System类中的currentTimeMillis()@Testpublicvoidtest1(){//返回当前时间与1970年1月......