work109.java
/**
*
*/
package test06;
/**
* @author user1
* 只包含一个成员的annotation类型
* 可用类型有String、Class、primitive、enumerate和annotation,以及前述类型的数组
* 只包含一个成员时,通常命名为value
*
* work109可替换为OneMemberAnnotation
*/
public @interface work109
{
String value();
}
work110.java
/**
*
*/
package test06;
/**
* @author user1
* work110 可替换为 MoreMemberAnnotation
* 包含多个成员的
*/
public @interface work110
{
String describe();
Class type();
}
work111.java
work112.java
/**
*
*/
package test06;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author user1
*
*/
//用于构造方法
(ElementType.CONSTRUCTOR)
//在运行时加载Annotation到JVM中
(RetentionPolicy.RUNTIME)
public work112
{
String value() default "默认构造方法"; //定义一个具有默认值的string成员
}
/*
* 在定义Annotation类型时,可以通过@Target来设置Annotation类型适用的程序元素种类。
* 未设置时,表示适用所有程序元素。
* 枚举类型ElementType用来设置@Target,含有
* ANNOTATION_TYPE ,用于Annotation类型
* TYPE ,用于类、接口和枚举
* CONSTRUCTOR ,用于构造函数
* FIELD ,用于成员变量和枚举
* METHOD ,用于方法
* PARAMETER ,用于参数
* LOCAL_VARIABLE ,用于局部变量
* PACKAGE ,用于包
*
*
*
* 通过Annotation类型的,@Retention可以设置有效范围。
* 枚举类型RetentionPolicy用来设置@Retention,含有
* SOURCE ,表示不编译Annotation到类文件中,有效范围最小
* CLASS ,表示编译Annotation到类文件中,但运行时不加载到JVM中
* RUNTIME ,表示在运行时加载Annotation到JVM中
*
*/
work113.jjavascriptava
/**
*
*/
package test06;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
/**
* @author user1
*
*/
//用于字段、方法和参数
({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
(RetentionPolicy.RUNTIME)
public work113
{
String descibe();
Class type() default void.class;
}
work114.java
package test06;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import test06.Anno.Constructor_Annotation;
import test06.Anno.Field_Method_Parameter_Annotation;
public class work114
{
//注释字段
(describe="编号",type=int.class)
int _id;
(describe="姓名",type=String.class)
String _name;
//采用默认值注释构造函数
()
public work114()
{
}
//注释带参数的构造函数
("立即初始化构造函数")
public work114(
(describe="编号",type=int.class)int id,
(describe="姓名",type=String.class) String name
)
{
this._id = id;
this._name = name;
}
//注释不带参数的方法
(describe="获取编号",type=int.class)
public int getId()
{
return this._id;
}
//注释带参数的方法
(describe="设置编号")
public void setId(
(describe="编号",type=int.class) int id)
{
this._id = id;
}
(describe="获取姓名",type=String.class)
public String getName()
{
return this._name;
}
(describe="设置姓名")
public void setName(
(describe="姓名",type=String.class) String name
)
{
this._name = name;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
}
}
class Anno
{
(ElementType.CONSTRUCTOR)
(RetentionPolicy.RUNTIME)
public Constructor_Annotation
{
String value() default "默认构造函数";
}
//用于字段、方法和参数
({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
(RetentionPolicy.RUNTIME)
public Field_Method_Parameter_Annotation
{
String describe(); //定义一个没有默认值的string类型成员
Class type() default Void.class; // 定义一个有默认值的Class成员
}
}
发表评论