英文:
Cannot find symbol of written method java.util.function
问题
我有类似以下代码:
public class Functionz {
public static boolean test() {
return true;
}
public static void main(String[] args) {
Function[] funcs = new Function[] {test}; // 以及其他函数
for (Function func : funcs) {
func();
}
}
}
我遇到的错误是:在函数数组声明的那一行出现了 cannot find symbol: test
错误。
希望这不是一个愚蠢的问题,我对 Java 很新,但不陌生于像 Python 和 C++ 这样的面向对象语言。
英文:
I have code like
public class Functionz {
public static boolean test() {
return true;
}
public static void main(String[] args) {
Function[] funcs = new Function[] {test}; // and others
for (Function func : funcs) {
func();
}
}
}
and my error is: cannot find symbol: test
in the line with the function array declaration.
Hope this isn't a stupid question, very new to java, not new to object oriented languages like python and C++.
答案1
得分: 2
在Java中,Function
表示一个带有一个输入参数和一个输出参数的函数。你可以像这样声明参数的类型:Function<Integer, String>
是一个将Integer
转换为String
的函数。而你的方法test()
不接受任何输入值,并且输出一个boolean
,所以它是一个Supplier
。
import java.util.function.Supplier;
public class Main {
public static boolean test() {
System.out.println("lorem ipsum");
return true;
}
public static void main(String[] args) {
Supplier[] funcs = new Supplier[] {Main::test}; // 和其他函数
for (Supplier func : funcs) {
func.get();
}
}
}
如果test
需要一个(且仅有一个)参数,那么你的代码可以编译通过,就像这样:
import java.util.function.Function;
public class Main {
public static boolean test(String str) {
System.out.println(str);
return true;
}
public static void main(String[] args) {
Function[] funcs = new Function[] {(Object anyObject) -> test(anyObject.toString())}; // 和其他函数
for (Function func : funcs) {
func.apply("lorem ipsum");
}
}
}
这是这些类型的列表。请注意,Function
在构造时不会为其参数指定类型,因为在Java中无法使用泛型类型创建数组(在特定情况下可能可以)=> 在这种情况下,使用List
会更有帮助。
英文:
A Function
in Java does takes one parameter as input and one as output.
You might declare parameter's type this way : Function<Integer, String>
is a function that transforms an Integer
into a String
Your method test()
does not take any input value and outputs a boolean
so it's a Supplier
.
import java.util.function.Supplier;
public class Main {
public static boolean test() {
System.out.println("lorem ipsum");
return true;
}
public static void main(String[] args) {
Supplier[] funcs = new Supplier[] {Main::test}; // and others
for (Supplier func : funcs) {
func.get();
}
}
}
Your code would compile if test requires one (and only one parameter) like
import java.util.function.Function;
public class Main {
public static boolean test(String str) {
System.out.println(str);
return true;
}
public static void main(String[] args) {
Function[] funcs = new Function[] {(Object anyObject) -> test(anyObject.toString())}; // and others
for (Function func : funcs) {
func.apply("lorem ipsum");
}
}
}
Here's the list of those types
Please note that Function
doesn't type its parameters in construction because you can't create arrays with generic type in Java (you might for specific usecases) => Use a List
will help you here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论