Function pointers/delegates in Java?
问题 For my Java game server I send the Action ID of the packet which basically tells the server what the packet is for. I want to map each Action ID (an integer) to a function. Is there a way of doing this without using a switch? 回答1: What about this one? HashMap<Integer, Runnable> map = new HashMap<Integer, Runnable>(); map.put(Register.ID, new Runnable() { public void run() { functionA(); } }); map.put(NotifyMessage.ID, new Runnable() { public void run() { functionB(); } }); // ... map.get(id)