Why doesn't Java have method delegates?

后端 未结 9 1512
再見小時候
再見小時候 2021-02-05 17:42

The Java gurunaths (natha नाथ = sanskrit for deity-master-protector) at Sun should condescend to accept the necessity of delegates and draft it into Java spec.

In C#, I

相关标签:
9条回答
  • 2021-02-05 18:21

    java does not have closures because it was not intended to be a functional language, but an object oriented one.
    as in many case you can fake another language paradigm, in this case using anonymous interfaces instead of closures.
    but now things are changing and under the pressure of new jvm languages like scala, groovy, jruby, etc., that combines oo and funcional paradigms, the java committee is trying to put closures into java 7.

    0 讨论(0)
  • 2021-02-05 18:25

    Maybe because if a method is to be passed and shared across objects, it shouldn't be owned by one single class. It should probably be shared via its own class. I mean, a transient function does feel a bit odd if you think of it. Bad OO I suppose.

    I REALLY like delegates for UI work. It makes window actions so much easier to program.

    It may come down to what you think is more negative, a function that has the wrong owner, or (in my case anyway) your code having methods that belong to one class (button clicks, window resize events) not being part of that one class.

    Not sure which I prefer.

    0 讨论(0)
  • 2021-02-05 18:29

    For whatever it's worth, I have implemented callback/delegate support in Java using reflection. Details and working source are available on my website.

    0 讨论(0)
提交回复
热议问题