将方法保存到变量中,java 8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29219984/
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
Save a method into a variable, java 8
提问by fangio
Is it possible to save a method into a variable?
I have a class which is called MyFilter and it filters Items on different fields.
The constructor of MyFilter should ask 2 things:
是否可以将方法保存到变量中?
我有一个名为 MyFilter 的类,它过滤不同字段上的项目。
MyFilter 的构造函数应该问两件事:
- String, for example filter Items by language, "English"
- This should be a method
- 字符串,例如按语言过滤项目,“英语”
- 这应该是一种方法
for example:
例如:
I have an Item and I want to check if the language == String given to the filter
So I need to get the language of that Item, so Item.getLanguage()...
I also need it for Item.getTitle(), Item.getId() and so on.
我有一个项目,我想检查是否提供给过滤器的语言 == 字符串
所以我需要获取该项目的语言,因此 Item.getLanguage()...
我也需要它用于 Item.getTitle(), Item.getId() 等等。
I think this is possible with lambda's but I don't know how.
我认为这对于 lambda 是可能的,但我不知道如何。
采纳答案by esin88
Yes, you can have a variable reference to any method. For simple methods it's usually enough to use java.util.function.
classes. For complex methods you have to define your @FunctionalInterface
with this method. Here's working example:
是的,您可以拥有对任何方法的变量引用。对于简单的方法,通常使用java.util.function.
类就足够了。对于复杂的方法,您必须@FunctionalInterface
使用此方法定义您的方法。这是工作示例:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Main {
public static void main(String[] args) {
final Consumer<Integer> simpleReference = Main::someMethod;
simpleReference.accept(1);
simpleReference.accept(2);
simpleReference.accept(3);
final ComplexInterface complexReference = Main::complexMethod;
final String complexResult = complexReference.someMethod("888", 321, new ArrayList<>());
System.out.println(complexResult);
}
private static void someMethod(int value) {
System.out.println(value);
}
private static String complexMethod(String stringValue, int intValue, List<Long> longList) {
final StringBuilder builder = new StringBuilder();
builder.append(stringValue).append(" : ").append(intValue);
for (Long longValue : longList) {
builder.append(longValue);
}
return builder.toString();
}
@FunctionalInterface
public static interface ComplexInterface{
String someMethod(String stringValue, int intValue, List<Long> longList);
}
}
回答by Razib
You can use method reference like -
您可以使用方法参考,如 -
System.out::println
Which is equivalent to the lambda expression -
这相当于 lambda 表达式 -
x -> System.out.println(x).
Moreover you can user reflection to store method and it will works for the earlier version of java
too.
此外,您可以使用反射来存储方法,它也适用于早期版本java
。
回答by axblount
You can use Java 8 method references. You can use the ::
'operator' to grab a method reference from an object.
您可以使用 Java 8 方法引用。您可以使用::
“运算符”从对象中获取方法引用。
import java.util.function.IntConsumer;
class Test {
private int i;
public Test() { this.i = 0; }
public void inc(int x) { this.i += x; }
public int get() { return this.i; }
public static void main(String[] args) {
Test t = new Test();
IntConsumer c = t::inc;
c.accept(3);
System.out.println(t.get());
// prints 3
}
}
You just need a @FunctionalInterface
that matches the signature of the method you want to store. java.util.function
contains a selection of the most commonly used ones.
您只需要一个@FunctionalInterface
与您要存储的方法的签名相匹配的。java.util.function
包含一些最常用的选择。