Java 没有函数名的方法如何与特定HashMap连接

Java 没有函数名的方法如何与特定HashMap连接,java,Java,请查看以下代码: private static Map< String, Method > supplierFunctionMap = new HashMap< String, Method >(); static { try { //ARP supplierFunctionMap.put( "206175-200", SupplierConfiguration.class.getDecla

请查看以下代码:

 private static Map< String, Method > supplierFunctionMap = new HashMap< String, Method >();
    static
    {
        try {
            //ARP
            supplierFunctionMap.put( "206175-200", SupplierConfiguration.class.getDeclaredMethod("asd", String.class));
            supplierFunctionMap.put( "206175-210", SupplierConfiguration.class.getDeclaredMethod("asd", String.class));
            supplierFunctionMap.put( "206175-998", SupplierConfiguration.class.getDeclaredMethod("asd", String.class));
            //ADD new suppliers below
        } catch (NoSuchMethodException e) {
            System.out.println("Error in Key / Value of Hashmap. Maybe Key or Value not existing or something else is wrong.");
            e.printStackTrace();
        }
    }
然后调用
getDeclared
方法


有人能解释一下为什么
是静态的吗{
可以这样声明,它是如何与
hashmap
连接的?

好吧,你的
供应商函数映射是
静态的
静态的
块只是将项目添加到
hashmap
静态的
变量中,任何
静态的
块都会在class装载

静态块未连接到
HashMap
,只是代码缩进错误。您可以将
静态块进一步向下放置


唯一的联系是
静态
块正在向相同的
HashMap

添加项。反射速度慢,难以调试,并且无法通过JIT编译器进行优化。最好使用
映射
supplierFunctionMap.get("206175-200").invoke(null, supplierNumberAndAdmnr);