Java的注解Annotation

在:java.lang.annotation

元注解:

元注解的作用就是负责注解其他注解。Java5.0定义的元注解:

  1. @Target
  2. @Retention
  3. @Documented
  4. @Inherited

@Target

作用:注解该用在什么地方

ElemenetType.CONSTRUCTOR—————————-构造器声明

ElemenetType.FIELD ————————————–域声明(包括 enum 实例)

ElemenetType.LOCAL_VARIABLE————————- 局部变量声明

ElemenetType.METHOD ———————————-方法声明

ElemenetType.PACKAGE ——————————— 包声明

ElemenetType.PARAMETER ——————————参数声明

ElemenetType.TYPE————————————— 类,接口(包括注解类型)或enum声明

@Target(ElementType.TYPE)
public @interface Table {
    /**
     * 数据表名称注解,默认值为类名称
     * @return
     */
    public String tableName() default "className";
}

@Target(ElementType.FIELD)
public @interface NoDBColumn {

}

@Retention

作用:该Annotation被保留的时间长短

RetentionPolicy.SOURCE ———————————只保留在源码中,不保留在class中,同时也不加载到虚拟机中

RetentionPolicy.CLASS ———————————-保留在源码中,同时也保留到class中,但是不加载到虚拟机中

RetentionPolicy.RUNTIME VM——-保留到源码中,同时也保留到class中,最后加载到虚拟机中

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
    public String name() default "fieldName";
    public String setFuncName() default "setField";
    public String getFuncName() default "getField"; 
    public boolean defaultDBValue() default false;
} 

@Documented

作用:是否会保存到 Javadoc 文档中

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
    public String name() default "fieldName";
    public String setFuncName() default "setField";
    public String getFuncName() default "getField"; 
    public boolean defaultDBValue() default false;
}

@Inherited

作用:标记注解,是否可以被继承,默认为 false

@Inherited
public @interface Greeting {
    public enum FontColor{ BULE,RED,GREEN};
    String name();
    FontColor fontColor() default FontColor.GREEN;
}

基本的注解

@Override: 表示该方法是重写父类中的方法,编译的时候会检查该方法,如果这个方法不是父类中存在的将会报错

@Deprecated: 表示该方法时已经过时的,表示该方法有风险或者有更好的替代方法

@SuppressWarnings: 表示在编译的时候忽略某种错误,如版本检查等

自定义注解

使用@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.以上所有类型的数组

import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
    String value() default "";
}

import java.lang.annotation.*;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {
    public enum Color {
        BLUE, RED, GREEN
    }

    Color fruitColor() default Color.BLUE;
}

public class Apple {
    @FruitName("Apple")
    String appleName;
    @FruitColor(fruitColor = FruitColor.Color.BLUE)
    String appleColor;

    public String getAppleName() {
        return appleName;
    }

    public void setAppleName(String appleName) {
        this.appleName = appleName;
    }

    public String getAppleColor() {
        return appleColor;
    }

    public void setAppleColor(String appleColor) {
        this.appleColor = appleColor;
    }

    public void disPlay() {
        System.out.println("appleName: " + appleName + " appColor: " + appleColor);
    }
}

参考:http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值