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);