注解+枚举

这篇博客详细介绍了Java中的枚举和注解。枚举用于定义程序中的固定值,枚举类型不能被外部实例化,常用于框架和组装响应内容。注解是一种元数据,用于提供额外的信息,如Mybatis、Servlet和Spring框架中的注解。同时讲解了自定义注解的创建和Junit单元测试的原理。
摘要由CSDN通过智能技术生成

注解+枚举

一、枚举

  1. 枚举定义?

    枚举:enumeration 关键字enum,表示一些程序中的固定值,是Java5.0之后提出的类型。

    枚举就是把程序中固定的值列举出来。

  2. 创建枚举?
    //和类对照着看
    //枚举本身不是类,但是我们可以看作是一个类来使用
    class A{
         }
    
    //枚举最大得特征就是属性全部大写
    enum A{
         
        RED,YELLOW,GREEN;
    }
    
  3. 枚举的使用?
    1. 枚举传入switch case中

      	@Test
          public void testEnum(){
             
              System.out.println(Color.RED);
              Color c = Color.RED;
              switch (c){
             
                  case GREEN:
                      System.out.println("绿灯");break;
                  case RED:
                      System.out.println("红灯");break;
                  case YELLOW:
                      System.out.println("黄灯");break;
              }
              System.out.println(Color.valueOf("RED")==Color.RED);
              System.out.println(Color.values()[0]+"\t"+Color.values()[1]+"\t"+Color.values
好的,下面是一个 Java 示例代码,使用注解+反射+枚举实现字典方法: ```java import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; @Retention(RetentionPolicy.RUNTIME) @interface DictionaryEntry { String key(); String value(); } enum Dictionary { FRUIT, COLOR, ANIMAL; } public class Main { private static Map<String, Map<String, String>> dictionaryMap = new HashMap<>(); public static void main(String[] args) { initDictionary(); String fruitName = "apple"; String fruitColor = getDictionaryValue(Dictionary.FRUIT, fruitName); System.out.println(fruitName + " is " + fruitColor); String colorName = "red"; String colorHex = getDictionaryValue(Dictionary.COLOR, colorName); System.out.println(colorName + " is " + colorHex); String animalName = "dog"; String animalSound = getDictionaryValue(Dictionary.ANIMAL, animalName); System.out.println(animalName + " says " + animalSound); } private static void initDictionary() { // Fruit dictionary Map<String, String> fruitMap = new HashMap<>(); fruitMap.put("apple", "red"); fruitMap.put("banana", "yellow"); fruitMap.put("orange", "orange"); dictionaryMap.put(Dictionary.FRUIT.name(), fruitMap); // Color dictionary Map<String, String> colorMap = new HashMap<>(); colorMap.put("red", "#FF0000"); colorMap.put("green", "#00FF00"); colorMap.put("blue", "#0000FF"); dictionaryMap.put(Dictionary.COLOR.name(), colorMap); // Animal dictionary Map<String, String> animalMap = new HashMap<>(); animalMap.put("dog", "woof"); animalMap.put("cat", "meow"); animalMap.put("bird", "tweet"); dictionaryMap.put(Dictionary.ANIMAL.name(), animalMap); } private static String getDictionaryValue(Dictionary dict, String key) { Map<String, String> dictMap = dictionaryMap.get(dict.name()); for (Map.Entry<String, String> entry : dictMap.entrySet()) { if (entry.getKey().equals(key)) { return entry.getValue(); } } return null; } static { for (Dictionary dict : Dictionary.values()) { Map<String, String> dictMap = new HashMap<>(); Class<?> dictClass; try { dictClass = Class.forName(dict.name()); } catch (ClassNotFoundException ex) { continue; } for (Field field : dictClass.getDeclaredFields()) { if (field.isAnnotationPresent(DictionaryEntry.class)) { DictionaryEntry entry = field.getAnnotation(DictionaryEntry.class); dictMap.put(entry.key(), entry.value()); } } dictionaryMap.put(dict.name(), dictMap); } } static class Fruit { @DictionaryEntry(key = "apple", value = "red") public static String APPLE; @DictionaryEntry(key = "banana", value = "yellow") public static String BANANA; @DictionaryEntry(key = "orange", value = "orange") public static String ORANGE; } static class Color { @DictionaryEntry(key = "red", value = "#FF0000") public static String RED; @DictionaryEntry(key = "green", value = "#00FF00") public static String GREEN; @DictionaryEntry(key = "blue", value = "#0000FF") public static String BLUE; } static class Animal { @DictionaryEntry(key = "dog", value = "woof") public static String DOG; @DictionaryEntry(key = "cat", value = "meow") public static String CAT; @DictionaryEntry(key = "bird", value = "tweet") public static String BIRD; } } ``` 这个例子,我们创建了一个枚举类型 `Dictionary`,表示三个不同的字典:`FRUIT`、`COLOR`、`ANIMAL`。我们使用注解 `@DictionaryEntry` 来标记每个字典的条目,然后使用反射初始化字典。 在 `initDictionary` 方法,我们创建了一个 `dictionaryMap`,包含了每个字典的名称和条目。我们使用反射枚举每个字典的条目,并将它们添加到 `dictionaryMap` 。 在 `getDictionaryValue` 方法,我们通过枚举类型 `Dictionary` 和键 `key` 获取字典。我们首先从 `dictionaryMap` 获取对应的字典,然后遍历字典的条目,查找与给定键匹配的条目并返回它的。 注意,这个例子只是一个简单的演示,实际应用可能需要更复杂的字典结构和查询方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值