java基础第25课

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

/**
*
*/
package test06;


/**
* @author user1
* work111 可替换为 DefaultValueAnnotation
* 带默认值
*/
public @interface work111
{
String describe() default "<默认值>";
Class type() default void.class;

}



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
*
*/
//用于构造方法
@Target(ElementType.CONSTRUCTOR)
//在运行时加载Annotation到JVM中
@Retention(RetentionPolicy.RUNTIME)
public @interface 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
*
*/
//用于字段、方法和参数
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface 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
{
//注释字段
@Field_Method_Parameter_Annotation(describe="编号",type=int.class)
int _id;
@Field_Method_Parameter_Annotation(describe="姓名",type=String.class)
String _name;

//采用默认值注释构造函数
@Constructor_Annotation()
public work114()
{

}

//注释带参数的构造函数
@Constructor_Annotation("立即初始化构造函数")
public work114(
@Field_Method_Parameter_Annotation(describe="编号",type=int.class)int id,
@Field_Method_Parameter_Annotation(describe="姓名",type=String.class) String name
)
{
this._id = id;
this._name = name;
}


//注释不带参数的方法
@Field_Method_Parameter_Annotation(describe="获取编号",type=int.class)
public int getId()
{
return this._id;
}

//注释带参数的方法
@Field_Method_Parameter_Annotation(describe="设置编号")
public void setId(
@Field_Method_Parameter_Annotation(describe="编号",type=int.class) int id)
{
this._id = id;
}

@Field_Method_Parameter_Annotation(describe="获取姓名",type=String.class)
public String getName()
{
return this._name;
}

@Field_Method_Parameter_Annotation(describe="设置姓名")
public void setName(
@Field_Method_Parameter_Annotation(describe="姓名",type=String.class) String name
)
{
this._name = name;
}

public static void main(String[] args)
{
// TODO Auto-generated method stub


}


}


class Anno
{
@Target(ElementType.CONSTRUCTOR)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constructor_Annotation
{
String value() default "默认构造函数";
}

//用于字段、方法和参数
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface Field_Method_Parameter_Annotation
{
String describe(); //定义一个没有默认值的string类型成员
Class type() default Void.class; // 定义一个有默认值的Class成员
}
}