什么是最接近Java有到.NET函数功能<>和行动<>代表?函数、最接近、行动、代表

2023-09-04 09:09:00 作者:没有公主命要有女王心

显然,Java没有委托或函数作为第一类值,并使用接口代替,但什么是最接近接口函数功能<>或动作<> .NET的代表?有Runnable接口和可赎回<>,但只是在口味不采取任何参数

Obviously, Java does not have delegates or functions as first class values and uses interfaces instead, but what is the closest interface to the Func<> or Action<> .NET delegates? There is Runnable and Callable<>, but only in flavors taking no parameters.

从Java不能有超载的类型具有相同的名称和不同数量的泛型类型参数,据我所知,不可能有一个单一的共享接口的名字,但有可能是一个Runnable1&LT;>,Runnable2&LT;>等

As Java cannot have overloaded types with the same name and different number of generic type arguments, I understand that there cannot be a single shared interface name, but there could be a Runnable1<>, Runnable2<> and so on.

这是风格的编程只是没有在Java中使用还是我失去了所有现有的接口?的

Is this style of programming just not used in Java or am I missing any existing interfaces?

推荐答案

我不是很熟悉的.Net,但是从我所看到的,到目前为止,似乎你通常会创建一个接口为每个类型的功能/动作要存储。这是不是真的一样,但听起来好像你在寻找什么。

I'm not very familiar with .Net, but from what I've seen so far it seems you would usually create an interface for each type of function/action you want to store. This is not really the same but sounds like what you're looking for.

(界面不是匿名的,它有一个名字,一个意思和文件,这可能使code更容易阅读。在另一方面,它是一个额外的麻烦,不得不写它,你不能只是扔在任何签名匹配功能)。

(the interface is not anonymous, it has a name, a meaning and documentation, which might make code easier to read. On the other hand it is an extra hassle to have to write it, and you cannot just throw in any function that matches the signature).

你有一个具体的用例,你正在寻找一个建议?这是很难做出一般性陈述。)

Do you have a specific use case for which you're seeking a recommendation? It's hard to make generalized statements ;).