Java的。参考没有参数的方法

时间:2018-02-28 13:25:58

标签: java java-8

我在理解与引用方法相关的Java 8时遇到一个问题,作为静态方法的参数。有我的代码,我找不到如何发送一个没有任何参数的方法的引用,并且必须是一个明确类的对象的方法。

所以,我希望我发送给静态函数的方法可以用于静态方法中的对象。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

interface FuncForPath<T> {
    T func(T path);
}

class MethodsForFolder {

    public static void printFilesInFolder(Path path, FuncForPath<Path> funcPath) {
        Stream<Path> paths = null;
        try {
            paths = Files.list(path);
        } catch (IOException e) {
            System.out.println(
                    "The problems have been appeared during reading folder: " + path.toAbsolutePath().toString());
            e.printStackTrace();
        }
        System.out.println("Files list in folder:" + path.toAbsolutePath().toString());
        paths.forEach(p -> funcPath.func(p).toString()); // I don't know to how to write this code to perform
    }
}

public class TestRefToIntance {

    public static String testWindowsFloder = "C://Logs";

    public static void main(String[] args) {

        Path path = Paths.get(testWindowsFloder);
        // I want that this 2 methods are performed depending on transfered methods
        // reference
        MethodsForFolder.printFilesInFolder(path, Path::toAbsolutePath);
        MethodsForFolder.printFilesInFolder(path, Path::getFileName);
    }
}

3 个答案:

答案 0 :(得分:1)

尝试使用Function<Path, Path>代替FuncForPath。这将需要一个采用Path参数并返回Path的方法。请注意,实例方法始终具有“不可见”this参数,因此Path::getFileName匹配该签名。

然后你会这样称呼它:paths.forEach( p -> funcPath.apply( p ).toString() );(虽然你没有对返回的字符串做任何事情,所以你可能想要调用paths.map( f ).map( Path::toString ).collect( someCollector );

答案 1 :(得分:0)

我的理解我已经解决了这个问题。下面有代码。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

interface FuncForPath<T> {
    T func(T path);
}

class MethodsForFolder {

    public static void printFilesInFolder(Path path, FuncForPath<Path> funcPath) {
        Stream<Path> paths = null;
        try {
            paths = Files.list(path);
        } catch (IOException e) {
            System.out.println(
                    "The problems have been appeared during reading folder: " + path.toAbsolutePath().toString());
            e.printStackTrace();
        }
        System.out.println("Files list in folder:" + path.toAbsolutePath().toString());
        paths.forEach(p -> System.out.println(funcPath.func(p).toString())); // I don't know to how to write this code to perform
    }
}

public class TestRefToIntance {

    public static String testWindowsFloder = "C://Logs";

    public static void main(String[] args) {

        Path path = Paths.get(testWindowsFloder);
        // I want that this 2 methods are performed depending on transfered methods
        // reference
        MethodsForFolder.printFilesInFolder(path, Path::toAbsolutePath);
        MethodsForFolder.printFilesInFolder(path, Path::getFileName);
    }
}

答案 2 :(得分:0)

另外,我删除了界面描述中的通用规范。

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

interface FuncForPath { // there is interface without type <T>
    Path func(Path path);
}

class MethodsForFolder {

    public static void printFilesInFolder(Path path, FuncForPath funcPath) {
        Stream<Path> paths = null;
        try {
            paths = Files.list(path);
        } catch (IOException e) {
            System.out.println(
                    "The problems have been appeared during reading folder: " + path.toAbsolutePath().toString());
            e.printStackTrace();
        }
        System.out.println("Files list in folder:" + path.toAbsolutePath().toString());
        paths.forEach(p -> System.out.println(funcPath.func(p).toString())); // I don't know to how to write this code
                                                                                // to perform
    }
}

public class TestRefToIntance {

    public static String testWindowsFloder = "C://Logs";

    public static void main(String[] args) {

        Path path = Paths.get(testWindowsFloder);
        // I want that this 2 methods are performed depending on transfered methods
        // reference
        MethodsForFolder.printFilesInFolder(path, Path::toAbsolutePath);
        MethodsForFolder.printFilesInFolder(path, Path::getFileName);
    }
}