Java equivalent of function mapping in Python

前端 未结 7 1429

In python, if I have a few functions that I would like to call based on an input, i can do this:

lookup = {\'function1\':function1, \'function2\':function2, \'fu         


        
7条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 15:30

    As mentioned in other questions, a Map with anonymous inner classes is one verbose way to do it.

    A variation is to use enums in place of the anonymous inner classes. Each constant of the enum can implement/override methods of the enum or implemented interface, much the same as the anonymous inner class technique but with a little less mess. I believe Effective Java 2nd Ed deals with how to initialise a map of enums. To map from the enum name merely requires calling MyEnumType.valueOf(name).

提交回复
热议问题