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

问题描述 投票:1回答:3

我有一个问题是理解与引用方法相关的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);
    }
}
java java-8
3个回答
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 );


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);
    }
}

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.