繁体   English   中英

爪哇。 不带参数的方法引用

[英]Java. Reference to method without parameters

我在理解 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);
    }
}

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM