无法找到已编写的方法 java.util.function。

huangapple go评论67阅读模式
英文:

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&lt;Integer, String&gt; 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(&quot;lorem ipsum&quot;);
        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) -&gt; test(anyObject.toString())}; // and others
        for (Function func : funcs) {
            func.apply(&quot;lorem ipsum&quot;);
        }
    }
}

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

huangapple
  • 本文由 发表于 2020年9月22日 00:08:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63996060.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定