Java equivalent of C# Delegates (queues methods of various classes to be executed)

前端 未结 3 1730
野性不改
野性不改 2020-12-31 14:59

TLDR:

Is there a Java equivalent of C#\'s delegates that will allow me to queue up methods of various classes and add them to the queue dynamically? Language constru

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-31 15:39

    Extracted from https://msdn.microsoft.com/en-gb/library/aa288459(v=vs.71).aspx :

    A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked. Unlike function pointers in C or C++, delegates are object-oriented, type-safe, and secure.

    That said, Java does not have delegates like C#. However, since Java 8, we do have some sort of function pointers by using method references and functional interfaces.

    As you politely requested, I am not going to tell you exactly how to implement this code, but you should be able to come up with a solution with this information.

提交回复
热议问题