Overloading in Java and multiple dispatch

前端 未结 6 1368
走了就别回头了
走了就别回头了 2020-11-28 09:16

I have a collection (or list or array list) in which I want to put both String values and double values. I decided to make it a collection of objects and using overloading o

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-28 09:46

    What you want is double or more general multiple dispatch, something that is actually implemented in other languages (common lisp comes to mind)

    Presumably the main reason java doesn't have it, is because it comes at a performance penalty because overload resolution has to be done at runtime and not compile time. The usual way around this is the visitor pattern - pretty ugly, but that's how it is.

提交回复
热议问题