注解:@interface 自定义注解的语法
来源:互联网 发布:域名注册ip查询 编辑:程序博客网 时间:2024/06/05 10:18
自定义注解:
使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。在定义注解时,不能继承其他的注解或接口。@interface用来声明一个注解,其中的每一个方法实际上是声明了一个配置参数。方法的名称就是参数的名称,返回值类型就是参数的类型(返回值类型只能是基本类型、Class、String、enum)。可以通过default来声明参数的默认值。
定义注解格式:
public @interface 注解名 {定义体}
注解参数的可支持数据类型:
1.所有基本数据类型(int,float,boolean,byte,double,char,long,short)
2.String类型
3.Class类型
4.enum类型
5.Annotation类型
6.以上所有类型的数组
Annotation类型里面的参数该怎么设定:
第一,只能用public或默认(default)这两个访问权修饰.例如,String value();这里把方法设为defaul默认类型;
第二,参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和 String,Enum,Class,annotations等数据类型,以及这一些类型的数组.例如,String value();这里的参数成员就为String;
第三,如果只有一个参数成员,最好把参数名称设为"value",后加小括号.例:下面的例子FruitName注解就只有一个参数成员。
简单的自定义注解和使用注解实例:
package annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 水果名称注解 * @author peida * */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface FruitName { String value() default "";}
package annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 水果颜色注解 * @author peida * */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface FruitColor { /** * 颜色枚举 * @author peida * */ public enum Color{ BULE,RED,GREEN}; /** * 颜色属性 * @return */ Color fruitColor() default Color.GREEN;}
package annotation;import annotation.FruitColor.Color;public class Apple { @FruitName("Apple") private String appleName; @FruitColor(fruitColor=Color.RED) private String appleColor; public void setAppleColor(String appleColor) { this.appleColor = appleColor; } public String getAppleColor() { return appleColor; } public void setAppleName(String appleName) { this.appleName = appleName; } public String getAppleName() { return appleName; } public void displayName(){ System.out.println("水果的名字是:苹果"); }}
注解元素的默认值:
注解元素必须有确定的值,要么在定义注解的默认值中指定,要么在使用注解时指定,非基本类型的注解元素的值不可为null。因此, 使用空字符串或0作为默认值是一种常用的做法。这个约束使得处理器很难表现一个元素的存在或缺失的状态,因为每个注解的声明中,所有元素都存在,并且都具有相应的值,为了绕开这个约束,我们只能定义一些特殊的值,例如空字符串或者负数,一次表示某个元素不存在,在定义注解时,这已经成为一个习惯用法。例如:
1 package annotation; 2 3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Retention; 6 import java.lang.annotation.RetentionPolicy; 7 import java.lang.annotation.Target; 8 9 /**10 * 水果供应者注解11 * @author peida12 *13 */14 @Target(ElementType.FIELD)15 @Retention(RetentionPolicy.RUNTIME)16 @Documented17 public @interface FruitProvider {18 /**19 * 供应商编号20 * @return21 */22 public int id() default -1;23 24 /**25 * 供应商名称26 * @return27 */28 public String name() default "";29 30 /**31 * 供应商地址32 * @return33 */34 public String address() default "";35 }
定义了注解,并在需要的时候给相关类,类属性加上注解信息,如果没有响应的注解信息处理流程,注解可以说是没有实用价值。如何让注解真真的发挥作用,主要就在于注解处理方法,下一步我们将学习注解信息的获取和处理!
- 注解:@interface 自定义注解的语法
- Java 自定义注解@interface
- 关于@interface 自定义注解
- @interface 自定义注解
- 使用@interface 自定义注解
- java @interface 自定义注解
- java自定义注解 @interface
- 自定义注解 @interface
- @interface 自定义注解
- @interface自定义注解的实际运用
- java之@interface-自定义注解
- Java自定义注解的语法要求
- (自定义注解)Java注释@interface的用法【转】
- 注解 @interface
- 注解的理解、自定义注解
- Java注解的使用@interface:
- Java注解@interface的用法
- Springmvc @interface注解的使用
- UFT自动化-参数化-导到excel提升
- 分享一个学习javaweb服务器的网站 ssm框架
- centos7中输入ifconfig出现ens33,没有eth0
- Hbuilder在Iphone上真机运行
- pat1016
- 注解:@interface 自定义注解的语法
- BZOJ 1468: Tree 点分治题解
- MapReduce的两种执行环境:本地测试环境,服务器环境
- Linux下实现免密码登录(超详细)
- C++知识点之深/浅拷贝
- 排序算法:快速排序
- Deeplearning 学习资料推介
- String:557. Reverse Words in a String III
- 07.node+express+mongodb 简单整合