疫情期间手机直线:18622734798
当前位置:首页网站设计:基础篇 → 全部信息
JAVA annotation入门
更新时间:2012/7/26 点击:1637次
一. 最常见的annotation
@Override:用在方法之上,用来告诉别人这一个方法是改写父类的
@Deprecated:建议别人不要使用旧的API的时候用的,编译的时候会用产生警告信息,可以设定在程序里的所有的元素上.
@SuppressWarnings:暂时把一些警告信息消息关闭
@Entity:表示该类是可持久化的类
二. 设计一个自己的Annotation
先看代码再讲话
1. 只有一个参数的Annotation实现

package chb.test.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;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {
String value();

2. 有两个参数的Annotation实现

package chb.test.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;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {
String description();
boolean isAnnotation();
}
  • 疫情期间手机直线:18622734798    服务邮箱:service@nfree.cn     QQ:1448132697
  • 地址:天津市河西区围堤道146号华盛广场B座22楼    

    津公网安备 12010302001042号

  • CopyRight 2006~2024 All Rights Reserved 天津市华易动力信息科技有限公司