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 ""; }
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; }
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 ""; }
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); } } } }
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("水果的名字是:苹果"); } }
/** * 输出结果 */ public class FruitRun { public static void main(String[] args) { FruitInfoUtil.getFruitInfo(Apple.class); } }
END 十期推荐 【261期】面试官:说出几个你熟悉的 Zookeeper 命令 【262期】面试官:谈谈MySQL主从复制的原理 【263期】面试最后一问:你有什么要问我的吗? 【264期】盘点MySQL主从复制,在面试中能被问什么? 【265期】面试官:为什么Integer用==比较时127相等而128不相等? 【266期】面试官:Redis主从集群切换数据丢失问题如何应对? 【267期】10道经典MySQL面试题 【268期】美团面试题:当你的JVM 堆内存溢出后,其他线程是否可继续工作? 【269期】链表高频面试题(包括反转、合并、相交、分割、环长等) 【270期】面试官:Spring的Bean实例化过程应该是怎样的? ? ~