Java等效于C#委托(排队要执行的各种类的方法) [英] Java equivalent of C# Delegates (queues methods of various classes to be executed)

查看:61
本文介绍了Java等效于C#委托(排队要执行的各种类的方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR:



是否有Java等效于C#的代表,这将允许我排队各种类的方法并将其动态添加到队列中吗?



上下文:



我以前使用过Unity 3D,并且喜欢诸如 Update 脚本方法之类的东西。只需声明该方法即可将其添加到每帧执行的方法列表中。我想在我的LWJGL游戏中创建类似的内容。为此,我想使用委托(或与其等效的东西)。是否有任何Java语言构造可以允许我执行此操作?我希望包含两个或多个(以便我可以选择最适合我的代码的)构造和使用它们的方式的答案。我不需要代码,只想知道从哪里开始。编程中最有趣的部分是解决问题,我也不想被剥夺。另外,我也不想被告知确切的操作方法。我想被引导向正确的方向,而不是被引导向那个目的地。我将如何学习? :-)

解决方案

https://msdn.microsoft.com/zh-CN/library/aa288459(v = vs.71).aspx


C#中的委托类似于C或C ++中的函数指针。使用
委托可以使程序员将对方法
的引用封装在委托对象中。然后可以将委托对象传递给
代码,该代码可以调用引用的方法,而不必在
编译时知道将调用哪个方法。与
C或C ++中的函数指针不同,委托是面向对象,类型安全和安全的。


,Java没有像C#这样的委托。 但是,从Java 8开始,我们确实通过使用方法引用和功能接口



如您所礼貌地要求,我不会确切告诉您如何实现此代码,但是您应该能够提出一种具有此信息的解决方案。 / p>

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 constructs instead of the code for it.

Context:

I have used Unity 3D before and I love things like the Update method of scripts. Just declaring the method adds it to the list of methods executed each frame. I want to create something like that in my LWJGL game. For this, I would want to use delegates (or something equivalent to it). Is there any Java language construct that would allow me to do this? I would prefer answers that include two or more (so that I can pick and choose which will be the most optimal for my code) constructs and a way of using them. I don't want the code, I just want to know where to start. The most fun part of programming is working the problem out and I don't want to be deprived of that. Also, I don't want to be told EXACTLY how to do it. I want to be guided in the right direction instead of being thrown in that direction onto that destination. How would I learn? :-)

解决方案

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.

这篇关于Java等效于C#委托(排队要执行的各种类的方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆