1.`package test;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
- 水果名称注解
*/
@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface FruitName {
String value() default “”;
}`
2.package test;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
-
水果颜色注解
/
@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface FruitColor {
/*- 颜色枚举
*/
public enum Color{ BLUE,RED,GREEN};
/**
- 颜色属性
*/
Color fruitColor() default Color.GREEN;
}
- 颜色枚举
3.package test;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
-
水果供应者注解
/
@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface FruitProvider {
/*- 供应商编号
*/
public int id() default -1;
/**
- 供应商名称
*/
public String name() default “”;
/**
- 供应商地址
*/
public String address() default “”;
}
- 供应商编号
package test;
import java.lang.reflect.Field;
/**
-
注解处理器
*/
public class FruitInfoUtil {
public static void getFruitInfo(Class<?> clazz){String strFruitName=" 水果名称:"; String strFruitColor=" 水果颜色:"; String strFruitProvicer="供应商信息:"; Field[] fields = clazz.getDeclaredFields(); for(Field field :fields){ if(field.isAnnotationPresent(FruitName.class)){ FruitName fruitName = (FruitName) field.getAnnotation(FruitName.class); strFruitName=strFruitName+fruitName.value(); System.out.println(strFruitName); } else if(field.isAnnotationPresent(FruitColor.class)){ FruitColor fruitColor= (FruitColor) field.getAnnotation(FruitColor.class); strFruitColor=strFruitColor+fruitColor.fruitColor().toString(); System.out.println(strFruitColor); } else if(field.isAnnotationPresent(FruitProvider.class)){ FruitProvider fruitProvider= (FruitProvider) field.getAnnotation(FruitProvider.class); strFruitProvicer=" 供应商编号:"+fruitProvider.id()+" 供应商名称:"+fruitProvider.name()+" 供应商地址:"+fruitProvider.address(); System.out.println(strFruitProvicer); } }
}
}
5.package test;
import test.FruitColor.Color;
/**
-
注解使用
*/
public class Apple {@FruitName(“Apple”)
private String appleName;@FruitColor(fruitColor=Color.RED)
private String appleColor;@FruitProvider(id=1,name=“陕西红富士集团”,address=“陕西省西安市延安路89号红富士大厦”)
private String appleProvider;public void setAppleColor(String appleColor) {
this.appleColor = appleColor;
}
public String getAppleColor() {
return appleColor;
}public void setAppleName(String appleName) {
this.appleName = appleName;
}
public String getAppleName() {
return appleName;
}public void setAppleProvider(String appleProvider) {
this.appleProvider = appleProvider;
}
public String getAppleProvider() {
return appleProvider;
}public void displayName(){
System.out.println(“水果的名字是:苹果”);
}
}
6.package test;
public class FruitRun {
public static void main(String[] args) {
FruitInfoUtil.getFruitInfo(Apple.class);
}
}
运行结果:
水果名称:Apple
水果颜色:RED
供应商编号:1 供应商名称:陕西红富士集团 供应商地址:陕西省西安市延安路89号红富士大厦