java什么是实例意思指的是
1. 首先看下定义的注解类:
类注解定义, MyClassAnnotation.java:
[java] view plain copy
package com.ross.annotation;
学习啦在线学习网 import java.lang.annotation.*;
/**
* Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com
* Date: 2012-1-29
* Since: MyJavaExpert v1.0
学习啦在线学习网 * Description: class annotation
*/
学习啦在线学习网 @Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.TYPE)
学习啦在线学习网 public @interface MyClassAnnotation
{
String uri();
String desc();
}
默认构造方法注解定义,MyConstructorAnnotation.java:
[java] view plain copy
package com.ross.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
学习啦在线学习网 import java.lang.annotation.Target;
/**
* Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com
学习啦在线学习网 * Date: 2012-1-29
* Since: MyJavaExpert v1.0
学习啦在线学习网 * Description: Constructor annotation
*/
@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.CONSTRUCTOR)
学习啦在线学习网 public @interface MyConstructorAnnotation
{
String uri();
学习啦在线学习网 String desc();
}
方法注解定义,MyMethodAnnotation.java:
[java] view plain copy
package com.ross.annotation;
学习啦在线学习网 import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
学习啦在线学习网 * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com
* Date: 2012-1-29
* Since: MyJavaExpert v1.0
* Description: method annotation
*/
@Retention (RetentionPolicy.RUNTIME)
学习啦在线学习网 @Target (ElementType.METHOD)
public @interface MyMethodAnnotation
{
String uri();
学习啦在线学习网 String desc();
}
字段注解定义, MyFieldAnnotation.java:
学习啦在线学习网 [java] view plain copy
学习啦在线学习网 package com.ross.annotation;
import java.lang.annotation.ElementType;
学习啦在线学习网 import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
学习啦在线学习网 import java.lang.annotation.Target;
/**
学习啦在线学习网 * Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com
学习啦在线学习网 * Date: 2012-1-29
学习啦在线学习网 * Since: MyJavaExpert v1.0
学习啦在线学习网 * Description: field annotation
*/
@Retention (RetentionPolicy.RUNTIME)
学习啦在线学习网 @Target (ElementType.FIELD)
public @interface MyFieldAnnotation
{
学习啦在线学习网 String uri();
String desc();
}
2. 再看下我们注解的应用和测试:
学习啦在线学习网 在类上面使用了MyClassAnnotation注解, 默认构造方法上使用了MyConstructorAnnotation注解, 自定义方法上使用了MyMethodAnnotation注解, 自定义字段上使用了MyFieldAnnotation注解, 在Mail函数中则实现了访问这些注解,并打印注解信息.
MySample.java:
[java] view plain copy
package com.ross.annotation;
学习啦在线学习网 import java.lang.reflect.*;
/**
* Author: Jiangtao He; Email: ross.jiangtao.he@gmail.com
* Date: 2012-1-29
* Since: MyJavaExpert v1.0
学习啦在线学习网 * Description: This class is used to show how to use the annotation of each level
*/
@MyClassAnnotation (uri = "com.ross.MySample" , desc = "The class name" )
public class MySample
{
学习啦在线学习网 @MyFieldAnnotation (uri = "com.ross.MySample#id" , desc = "The class field" )
public String id;
/**
* Description: default constructor
*/
学习啦在线学习网 @MyConstructorAnnotation (uri = "com.ross.MySample#MySample" , desc = "The default constuctor" )
public MySample()
{
}
/**
* Description: normal method
*/
学习啦在线学习网 @MyMethodAnnotation (uri = "com.ross.MySample#setId" , desc = "The class method" )
public void setId(String id)
{
this .id = id;
}
/**
* Description: MyAnnotation test
* @throws NoSuchMethodException
* @throws SecurityException
学习啦在线学习网 * @throws NoSuchFieldException
*/
public static void main(String[] args) throws SecurityException,
学习啦在线学习网 NoSuchMethodException, NoSuchFieldException
{
MySample oMySample = new MySample();
// get class annotation
学习啦在线学习网 MyClassAnnotation oMyAnnotation = MySample.class
学习啦在线学习网 .getAnnotation(MyClassAnnotation.class );
学习啦在线学习网 System.out.println("Class's uri: " + oMyAnnotation.uri() + "; desc: "
学习啦在线学习网 + oMyAnnotation.desc());
// get constructor annotation
Constructor oConstructor = oMySample.getClass().getConstructor();
学习啦在线学习网 MyConstructorAnnotation oMyConstructorAnnotation = (MyConstructorAnnotation) oConstructor
学习啦在线学习网 .getAnnotation(MyConstructorAnnotation.class );
学习啦在线学习网 System.out.println("Constructor's uri: "
学习啦在线学习网 + oMyConstructorAnnotation.uri() + "; desc: "
学习啦在线学习网 + oMyConstructorAnnotation.desc());
学习啦在线学习网 // get method annotation
Method oMethod = oMySample.getClass().getDeclaredMethod("setId" ,String. class );
学习啦在线学习网 MyMethodAnnotation oMyMethodAnnotation = oMethod
学习啦在线学习网 .getAnnotation(MyMethodAnnotation.class );
System.out.println("Method's uri: " + oMyMethodAnnotation.uri()
+ "; desc: " + oMyMethodAnnotation.desc());
学习啦在线学习网 // get field annotation
学习啦在线学习网 Field oField = oMySample.getClass().getDeclaredField("id" );
学习啦在线学习网 MyFieldAnnotation oMyFieldAnnotation = oField
学习啦在线学习网 .getAnnotation(MyFieldAnnotation.class );
学习啦在线学习网 System.out.println("Field's uri: " + oMyFieldAnnotation.uri()
+ "; desc: " + oMyFieldAnnotation.desc());
}
}
学习啦在线学习网 控制台打印结果:
[plain] view plain copy
学习啦在线学习网 Class's uri: com.ross.MySample; desc: The class name
学习啦在线学习网 Constructor's uri: com.ross.MySample#MySample; desc: The default constuctor
Method's uri: com.ross.MySample#setId; desc: The class method
Field's uri: com.ross.MySample#id; desc: The class field
至此本实例就完成了, 其实就是抓住两点一个是定义注解类,另外一个是如何访问注解, 就算是学会了.
java什么是实例意思指的是




