java 如何在java中将成员函数作为参数传递?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15228820/
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
How to pass a member function as a parameter in java?
提问by Micha Wiedenmann
My collect()
function calls Foo.f()
. I would like to make Foo.f()
itself a parameter of my function. Is this possible in Java?
我的collect()
函数调用Foo.f()
. 我想让Foo.f()
自己成为我的函数的参数。这在Java中可能吗?
- How can I pass either
Foo.f()
orFoo.g()
(or any other function ofFoo
that returns aString
) to my function? - Is there an already existing function which walks a collection and collects the result of calling a method of every collection item?
- 如何将
Foo.f()
orFoo.g()
(或Foo
返回 a 的任何其他函数String
)传递给我的函数? - 是否有一个已经存在的函数来遍历集合并收集调用每个集合项的方法的结果?
.
.
class Foo {
public String f() { return "f"; }
public String g() { return "g"; }
// ...
}
public List<String> collect(List<Foo> foos)
{
List<String> result = new ArrayList<String>();
for (final Foo foo: foos) {
result.add(foo.f()); // I want Foo.f to be a parameter
}
return result;
}
Update
更新
I would like to point out the fact that I am not merely calling the same function but rather the member function f
for all items of the List<Foo>
collection.
我想指出一个事实,即我不仅调用同一个函数,而且调用集合中f
所有项目的成员函数List<Foo>
。
回答by irreputable
In Java 8, you can do
在 Java 8 中,你可以这样做
collect(foos, Foo::f);
public List<String> collect(List<Foo> foos, Function<Foo,String> func)
{
List<String> result = new ArrayList<String>();
for (final Foo foo: foos) {
result.add(func.apply(foo));
}
return result;
}
Or with steam API
或使用蒸汽 API
Stream<Foo> foos = ...;
Stream<String> strs = foos.map(Foo::f);
回答by Sleiman Jneidi
You can use interfaces
您可以使用接口
interface Foo
{
String fn();
}
and pass the interface to the method
并将接口传递给方法
void anyMethod(Foo f)
{
f.fn();
}
you dont need to create a concrete Foo
, just create Foo
anonymously
你不需要创建一个具体的Foo
,只需Foo
匿名创建
new Foo() {
@Override
public String fn()
{
return "something";
}
};
In Java 8 you don't need to anonymously implement the interface. You could use a lambda expression instead.
在 Java 8 中,您不需要匿名实现接口。您可以改用 lambda 表达式。
anyMethod(()-> "something");
回答by Nikolay Kuznetsov
In Java you are not supposed to pass method as a parameter, but you can pass an object as parameter.
在 Java 中,您不应该将方法作为参数传递,但可以将对象作为参数传递。
It is called Strategy Pattern, you would need to use interface.
它称为Strategy Pattern,您需要使用接口。
回答by Esailija
You can use an interface like:
您可以使用如下接口:
interface IFoo {
String getString();
}
Then implement it in the custom ways:
然后以自定义方式实现它:
class F implements IFoo {
public String getString() {
return "f";
}
}
class G implements IFoo {
public String getString() {
return "g";
}
}
And have your function take a list of anything that implements IFoo
:
并让您的函数列出实现的任何内容IFoo
:
public List<String> collect(List<? extends IFoo> foos)
{
List<String> result = new ArrayList<String>();
for (final IFoo foo: foos) {
result.add(foo.getString());
}
return result;
}
Usage:
用法:
for (String a : collect(Arrays.asList(new F(), new G()))) {
System.out.println(a);
}
//Prints f and g