重学java 85.Java反射、注解、枚举总结

凡事往好处想,人生便会豁达

                                       —— 24.6.24

一、Junit单元测试

1.概述:

        Junit是一个单元测试框架,可以代替main方法去执行其他的方法

2.使用

a.导入jar包

b.注解

        @Test —— 单独执行一个方法

        @Before —— 在@Test之前执行.有多少个@Test执行.@Before对应的方法就执行多少次

        @After —— 在@Test之后执行,有多少个@Test执行.@After对应的方法就执行多少次

二、类的加载机制

1.类的加载时机

a.new对象
b.new子类对象
c.执行main方法
d.调用静态成员
e.反射

2.类的加载机制 —— 双亲委派

三、反射

1.概述 -- 解剖class对象的技术

2.问题:能解剖class对象的啥呢?

a.解剖出成员变量:赋值

b.解剖出成员方法: 调用

c.解剖出构造方法: new对象

3.作用 -- 写出来的代码重灵活,更通用

4.获取Class对象

a.对象.getClass()

b.类名.class

c.Class.forName("类的全限定名")

5.反射构造

Constructor<?>[] getConstructors() ——> 获取所有public的构造

Constructor<T> getConstructor(Class<?>...parameterTypes) ——> 获取指定的public的构造

TnewInstance(Object...initargs) ——> 创建对象

Class类中的方法:new对象的快捷方式 —— T newInstance() ——> 根据空参构造创建对象

Constructor<?> getDeclaredConstructors() ——> 获取所有构造方法,包括private

Constructor<T> getDeclaredConstructor(类<?>...parameterTypes) ——> 获取指定构造,包括private

void setAccessible(boolean flag) ——> 修改访问权限

6.反射成员方法

a. Method[] getMethods() ——> 获取所有public的方法,包括父类中的public方法

b.Method getMethod(String name, Class<?..parameterTypes) 获取指定的public的成员方法

c.Object invoke(Object obj, Object... args) ——> 执行方法

d.Method[] getDeclaredMethods() ——> 获取所有的成员方法,包括private 的

e.Method getDeclaredMethod(String name,类<?>... parameterTypes) ——> 获取执行成员方法包括private

7.反射属性

a.Field[] getFields() ——> 获取所有public的属性
b.Field[] getDeclaredFields() ——> 获取所有属性,包括priavte的
c.Field getField(String name) ——> 获取指定public的属性
d.Field getDeclaredField(String name) ——> 获取指定属性,包括priavte的
e.void set(Object obj,Object value) ——> 为属性赋值,相当于javabean中的set方法
f.Object get(Object obj) ——> 获取属性值

四、注解

1.定义注解

 public @interface 注解名{}

2.定义属性

a.数据类型 属性名() ——> 此属性没有默认值,需要在使用注解的时候为其赋值
b.数据类型 属性名0) default 值 ——> 此属性有默认值,如果有需要,还可以二次赋值

3.注解的使用

1.注解的使用 —— 说白了就是为注解中的属性赋值
2.使用位置上 —— 在类上使用,方法上使用,成员变量上使用,局部变量上使用,参数位置使用等
3.使用格式:a.@注解名(属性名 =值,属性名=值...)
                     b.如果属性中有数组:@注解名(属性名 ={元素1,元素2...})

4.注解解析

a.boolean isAnnotationPresent(Class<?extends Annotation> annotationClass) ——> 判断指定位置上有没有指定的注解

b.getAnnotation(Class<T>annotationClass) ——> 获取指定的注解,返回值类型为获取的注解类型

5.元注解

a.概述 —— 控制注解的注解

b.@Target:控制注解的使用位置

c.@Retention:控制注解的生命周期加加载位置)

五、枚举

1.定义

public enum 枚举类名{}

2.枚举值

a.枚举值特点:都是static final,但是定义的时候不要写出来,写出来报错
写完所有的枚举值之后,最后加个;

枚举值名字要大写 ——> 开发习惯

b.使用:类名直接调用
c.注意:每一个枚举值都是当前枚举类的对象

3.构造 —— 都是private修饰的

4.使用场景 —— 表示对象的状态

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个 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、付费专栏及课程。

余额充值