Java 在地图上迭代时使用反射

Java 在地图上迭代时使用反射,java,reflection,map,iterator,Java,Reflection,Map,Iterator,我不知道如何使用反射来调用这个内部方法,下面是我的想法。但是,它不起作用: interface Foo { void callMe(); } public class Tester{ public TreeMap<Integer, Foo> testMap = new TreeMap<Integer, Foo>(); public void foo(Foo foo, Integer d) { testMap.put(d, foo); } p

我不知道如何使用反射来调用这个内部方法,下面是我的想法。但是,它不起作用:

interface Foo {
   void callMe();
} 

 public class Tester{

 public TreeMap<Integer, Foo> testMap = new TreeMap<Integer, Foo>();

 public void foo(Foo foo, Integer d) {
    testMap.put(d, foo);
}

 public void testCase() { 
    for (Integer key: testMap.keySet()) {
       testMap.get(key).callMe(d);   
    }
}

public static void main(String[] args) {

 Tester testUser = new tester();

 testUser.foo(new Foo(){ void callMe(Integer d){ 
   System.out.println("Test " + d); } 
  }, 5);

testUser.foo(new Foo(){ void callMe(Integer d){ 
   System.out.println("Test Two " + d); } 
  }, 10);

   testUser.testCase();

 }
}
接口Foo{
无效callMe();
} 
公共类测试员{
publictreemap testMap=newtreemap();
公共void foo(foo foo,整数d){
testMap.put(d,foo);
}
public void testCase(){
for(整数键:testMap.keySet()){
testMap.get(key).callMe(d);
}
}
公共静态void main(字符串[]args){
Tester testUser=新测试仪();
testUser.foo(新的foo(){void callMe(整数d){
System.out.println(“Test”+d);}
}, 5);
testUser.foo(新的foo(){void callMe(整数d){
System.out.println(“测试二”+d);}
}, 10);
testUser.testCase();
}
}
我必须对代码进行哪些更改才能运行?

a)这不是反射


b) 您应该使用
testUser.foo(…)
而不是
tester.foo(…)

那么它是什么?我试图将一个接口传递给树映射的值,并执行该接口的内部方法,该接口大约是一个回调对象。(无论如何,流行语被高估了。)……我在第二部分中提出的建议是主要的。其他一切看起来都很好。