java annotation使用
annotation使用
2010年2月11日 辽国胖胖
今天看annotation规范,特记录如下,以便以后参考,也供初学的朋友们一个可运行案例,有不妥之处,请指正修改;另外,特别声明,如果转载或者引用请注明出处。
介绍
Annotation是java5新添加的特性, annotation是一种元数据Metadata,其有如下特点:
² 添加删除annotation不能对程序运行产生任何影响
² 对IDE开发环境以及library产生影响
² 通过reflect可以在source files, class files以及jvm运行时得到annotation信息
² 对javadoc工具的一种补充,在产生文档的时候使用javadoc,其它情况下可以使用annotation
² 由于annotation是在java类中等于和使用,减少了java程序员学习的曲线,减少了程序员学习的负担,有可能在以后一段时间内有可能取代其它描述元数据的形式,比如dtd
定义
Annotation定义
和普通的interface定义几乎一样,就在interface前加一个@符号,不知道sun为啥不弄一个annotation之类的关键字来代替@interface,感觉很别扭,有知道详情的朋友请告诉我一声,例如定义如下:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String myvalue();
}
Annotation定义注意:
1、 修饰符只能为public 或者abstract
2、 @interface不能把@符号同interface分开
3、 有两个重要的annotation需要说明Target、Retention,其值具体参考《系统annotation部分》
属性方法的定义
比如上面MyAnnotation就定义了一个myvalue方法(即annotation的一个属性),一个annotation中可以定义多个方法。
定义方法注意:
1、 不能有任何参数,否则报Annotation attributes cannot have parameters
2、 不能抛任何异常,否则报Syntax error on token "throws", default expected
3、 返回类型只能为primitives, String, Class, enums, annotations, 以及前面数组类型,比如把上面的myvalue()方法的返回改为Object,报Invalid type Object for the annotation attribute M
相关文档:
Java时间设为二十四小时制和十二小时制的区别:
1) 二十四小时制: “yyyy-MM-dd HH:mm:ss”
2)十二小时制: “"yyyy-MM-dd hh:mm:ss"”
例(二十四小时制):
private String getTime(){
Calendar now;
SimpleDateFormat fmt;
now = Calendar.getInstance();
fmt = new S ......
http://writeblog.csdn.net/PostEdit.aspx?entryId=5305099
Contents
Preliminary material
Method One: A simple approach
Method Two: A less simple approach
Java for the desktop user
Java for the developer and the server environment
Requirements
Step 1. Initial setup
Step 2. Installing your favor ......
final的作用随着所修饰的类型而不同
1、final修饰类中的属性或者变量
无论属性是基本类型还是引用类型,final所起的作用都是变量里面存放的“值”不能变。
&n ......
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:Verdana;
panose ......
http://www.ibm.com/developerworks/cn/java/j-lo-uidsl/index.html
基于 Java 的界面布局 DSL 的设计与实现
级别: 中级
孙 鸣
邓 辉
2007 年 9 月 11 日
界面设计应该是一项充满创造性、富有乐趣的工作,但是却往往被认为非常的枯燥和繁琐。究其原因,是因为界面布局领域所采用的描 ......