java java中最接近函数指针的东西是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4685435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the cloest thing to a function pointer in java?
提问by user489041
Possible Duplicate:
What's the nearest substitute for a function pointer in Java?
可能的重复:
Java 中最接近函数指针的替代品是什么?
I just have a situation where it would be nice to have the functionality of something similar to function pointers like you might use in c or c++. The best thing I can think of is combining Runnables and launch them in a different thread. Suggestions?
我只是有一种情况,如果拥有类似于函数指针的功能,就像您可能在 c 或 c++ 中使用的那样,那会很好。我能想到的最好的事情是组合 Runnables 并在不同的线程中启动它们。建议?
回答by Bozho
Until they add closures to java, the thing seems closest to java.lang.reflect.Method
. For example Method method = SomeClass.getMethod("methodName", methodParameterTypes)
在他们向 java 添加闭包之前,这件事似乎最接近java.lang.reflect.Method
. 例如Method method = SomeClass.getMethod("methodName", methodParameterTypes)
Then you can pass the method
around (like a function pointer) and call invoke(..)
on it, passing an object (or null
if static)
然后你可以传递method
周围(就像一个函数指针)并调用invoke(..)
它,传递一个对象(或者null
如果是静态的)
But this is not quite a good practice. You can define an interface, or use an existing one Runnable
/ Callable
, implement it inline, and pass the instance.
但这不是一个很好的做法。您可以定义一个接口,或使用现有的Runnable
/ Callable
,内联实现它,然后传递实例。
回答by helpermethod
Use Functors, aka Function Objects.
使用Functors,又名函数对象。
Java has no first-class functions, so function objects are usually expressed by an interface with a single method (most commonly the Callable interface), typically with the implementation being an anonymous inner class.
Java 没有一等函数,因此函数对象通常由具有单个方法的接口(最常见的是 Callable 接口)表示,通常实现是匿名内部类。
回答by rsp
You might be looking for the command pattern, which essentially defines an interface with one method (say execute()
) and classes implementing this method for different actions. You can use references to instances of these classes kinda like function pointers in C++. This pattern is often used to implement interpreters.
您可能正在寻找命令模式,它本质上定义了一个接口,其中包含一个方法(比如execute()
)和为不同操作实现此方法的类。您可以使用对这些类的实例的引用,有点像 C++ 中的函数指针。这种模式通常用于实现解释器。
回答by sleske
One option is using java.lang.reflect.Method
, as explained in Bozho's answers. It's very flexible, but the main drawbacks are that you lose all type safety - there is no check that you are calling the method with the right parameters (and expect the right return type).
一种选择是使用java.lang.reflect.Method
,如 Bozho 的回答中所述。它非常灵活,但主要缺点是您失去了所有类型安全性 - 没有检查您是否使用正确的参数调用方法(并期望正确的返回类型)。
So if you later change the method, and forget to change all callers, you'll only find out at runtime (through a ClassCastException
). Even the method name is only checked at runtime (because you fetch it by name.
因此,如果您稍后更改该方法,而忘记更改所有调用者,则只能在运行时(通过ClassCastException
)发现。甚至方法名称也只在运行时检查(因为您是按名称获取的。
An alternative is to use callbacks via interfaces. A method that wants to accept another method as a parameter just expects an instance of a certain interface. The interface only has one method, doStuff
. Then you can create instances that implement the interface, and pass those to the methods. That's a bit more verbose (because you need the interface), but typesafe and thus easier to refactor.
另一种方法是通过接口使用回调。想要接受另一个方法作为参数的方法只需要某个接口的实例。该接口只有一种方法,doStuff
. 然后您可以创建实现接口的实例,并将它们传递给方法。这有点冗长(因为您需要接口),但类型安全,因此更容易重构。
That is more or less the command pattern, BTW.
这或多或少是命令模式,顺便说一句。
回答by Cratylus
Callbacks are a way to implement something similar what you do with a function pointer in C++.
Additionally they are type-safe (unlike function pointers which might not be).
回调是一种实现类似于在 C++ 中使用函数指针所做的事情的方法。
此外,它们是类型安全的(与可能不是的函数指针不同)。