博客
关于我
java 注解——Annotations Basics
阅读量:302 次
发布时间:2019-03-03

本文共 1957 字,大约阅读时间需要 6 分钟。

// 注解的格式

The Format of an Annotation

// 一个简单的注解如下所示

In its simplest form, an annotation looks like the following:

@Entity

// @这个标识符告诉编辑器这是一个注解,下面以重写(Override)这个注解为例

The at sign character (@) indicates to the compiler that what follows is an annotation. In the following example, the annotation's name is Override:

@Overridevoid mySuperMethod() { ... }

// 注解可以包括属性(elements),这些属性可以是已命名的也可以的未命名的,而且他们可以有自己的属性值(values

The annotation can include elements, which can be named or unnamed, and there are values for those elements:

@Author(   name = "Benjamin Franklin",   date = "3/27/2003")class MyClass() { ... }

or

@SuppressWarnings(value = "unchecked")void myMethod() { ... }

// 如果注解只有一个属性,而且属性名为value,那么属性名可以省略,正像下面这个例子展示的那样

If there is just one element named value, then the name can be omitted, as in:

@SuppressWarnings("unchecked")void myMethod() { ... }

//  如果注解没有属性,那么括号也可以省略,比如前面@Override的例子

If the annotation has no elements, then the parentheses can be omitted, as shown in the previous @Override example.

// 同一个位置可以使用多个注解

It is also possible to use multiple annotations on the same declaration:

@Author(name = "Jane Doe")@EBookclass MyClass { ... }

// 如果是多个同类注解,这种情况被称作多重注解

If the annotations have the same type, then this is called a repeating annotation:

@Author(name = "Jane Doe")@Author(name = "John Smith")class MyClass { ... }

// Java SE 8 release 开始支持多重注解,想了解更多,请点击链接

Repeating annotations are supported as of the Java SE 8 release. For more information, see .

// 注解可以是Java SE API里面java.lang或者java.lang.annotation这些包里面已经定义的类型,比如前面提到的Override和SuppressWarnings都是

// java预定义注解。也可以是自定义类型,前面例子展示的Author和Ebook都是这一类。

The annotation type can be one of the types that are defined in the java.lang or java.lang.annotation packages of the Java SE API. In the previous examples,Override and SuppressWarnings are . It is also possible to define your own annotation type. The Author and Ebook annotations in the previous example are custom annotation types.

转载地址:http://mdtm.baihongyu.com/

你可能感兴趣的文章
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>
MySQL、Redis高频面试题汇总
查看>>
MYSQL、SQL Server、Oracle数据库排序空值null问题及其解决办法
查看>>
mysql一个字段为空时使用另一个字段排序
查看>>
MySQL一个表A中多个字段关联了表B的ID,如何关联查询?
查看>>
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL不会性能调优?看看这份清华架构师编写的MySQL性能优化手册吧
查看>>
MySQL不同字符集及排序规则详解:业务场景下的最佳选
查看>>
Mysql不同官方版本对比
查看>>
MySQL与Informix数据库中的同义表创建:深入解析与比较
查看>>
mysql与mem_细说 MySQL 之 MEM_ROOT
查看>>