Java Lambda表达式是否利用“隐藏”?或本地包裹进口? [英] Do Java Lambda Expressions Utilize "Hidden" or Local Package Imports?

查看:179
本文介绍了Java Lambda表达式是否利用“隐藏”?或本地包裹进口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于lambda表达式似乎采用的Java包的明显隐藏或本地导入。

This question is about the apparent "hidden" or local imports of Java packages that lambda expressions seem to employ.

以下示例代码编译并运行正常(它只列出给定目录中的文件):

The following sample code compiles and runs fine (it just lists the files in the given directory):

package com.mbm.stockbot;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Temp2 {
    public static void main(String[] args) {
        Temp2 t = new Temp2();
        t.readDir();
    }

    public void readDir() {
        try {
            Files.walk(Paths.get("C:/Users/mbmas_000/Downloads/SEC Edgar"), 1).forEach(filePath -> {
                if (Files.isRegularFile(filePath)) {
                    System.out.println(filePath);
                }
            });
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

注意变量 filePath Path 的一个实例,我认为它的实现包含在包 java.nio.file中.Path ,虽然该软件包没有 import

Note that the variable filePath is an instance of Path, whose implementation I think is contained in package java.nio.file.Path, although there is no import for that package.

现在,如果我做了一个小修改,比如将 System.out.println 的调用重构为它自己的方法:

Now, if I make a small modification, say by refactoring the call to System.out.println to its own method:

package com.mbm.stockbot;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Temp2 {

    public static void main(String[] args) {
        Temp2 t = new Temp2();
        t.readDir();
    }

    public void readDir() {
        try {
            Files.walk(Paths.get("C:/Users/mbmas_000/Downloads/SEC Edgar"), 1).forEach(filePath -> {
                if (Files.isRegularFile(filePath)) {
                    printPath(filePath);
                }
            });
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    public void printPath(Path passedFilePath) {
        System.out.println(passedFilePath);
    }
}

我现在必须'导入' import java.nio.file.Path ,否则我收到编译错误。

I must now 'import' import java.nio.file.Path, otherwise I get a compiler error.

所以我的问题是:


  1. 如果 filePath 确实是 java.nio.file的实例.Path ,为什么我需要在第一个例子中导入它,

  1. If filePath is indeed an instance of java.nio.file.Path, why don't I need to import it in the first example, and

如果使用lambda表达式执行隐藏下的导入,那么为什么我需要在创建一个方法时添加 import 一个 Path 的实例作为参数?

If using the lambda expression performs the import "under the covers," then why do I need to add the import when I create a method that takes an instance of Path as an argument?

方法可用于调用 filePath passedFilePath 是相同的,这让我相信它们都是<$ c $的实例c> java.nio.file.Path 。

The methods available to call on both filePath and passedFilePath are identical, leading me to believe they are both instances of java.nio.file.Path.

推荐答案

import 声明并不意味着声明您的代码正在使用哪些类;他们只是声明用于解析非限定标识符的内容。因此,如果您在代码中使用非限定标识符 Path ,则必须使用 import java.nio.file.Path; 声明它应该被解析为此限定类型。顺便说一下,这不是解决名称的唯一方法。名称也可以通过类继承来解决,例如如果它们与继承的成员类的简单名称匹配。

import declarations are not meant to declare what classes your code is using; they just declare what to use to resolve unqualified identifiers. So if you are using the unqualified identifier Path in your code you have to use import java.nio.file.Path; to declare that it should get resolved to this qualified type. This is not the only way to resolve a name, by the way. Names can also get resolved through the class inheritance, e.g. if they match the simple name of an inherited member class.

如果您在不引用其名称的情况下隐式使用某种类型,则不需要 import 语句,不仅限于lambda表达式,它甚至不是特殊的Java 8功能。例如。 with

If you are using a type implicitly without referring to its name you don’t need an import statement, that’s not limited to lambda expressions, it is not even a special Java 8 feature. E.g. with

Files.walk(Paths.get("C:/Users/mbmas_000/Downloads/SEC Edgar"), 1)

您已经隐式使用路径类型因为它是 Paths.get 的返回类型,参数类型为 Files.walk ,换句话说,你是接收 java.nio.file.Path 的实例并将其传递给另一个方法而不引用其类型名称,因此您不需要进口。此外,您调用varargs方法接受任意数量的 FileVisitOption 实例。您没有指定任何内容,因此您的代码将创建一个零长度 FileVisitOption [] 数组并将其传递给 Files.walk ,再次,没有 import

you are already using the Path type implicitly as it’s the return type of Paths.get and a parameter type of Files.walk, in other words, you are receiving an instance of java.nio.file.Path and passing it to another method without referring to its type name, hence you don’t need an import. Further you are calling a varargs method accepting an arbitrary number of FileVisitOption instances. You are not specifying any, therefore your code will create a zero-length FileVisitOption[] array and pass it to Files.walk, again, without an import.

使用改进的类型推断,还有另一种可能性类型而不参考其名称,例如如果你打电话:

With the improved type inference, there is another possibility to use a type without referring to its name, e.g. if you call:

Files.newByteChannel(path, new HashSet<>());

您不仅要创建零长度 FileAttribute [] varargs参数的数组没有按名称引用此类型,您还创建了 HashSet< OpenOption> 而没有引用类型 OpenOption 按名称。所以这也不需要,导入 java.nio.file.attribute.FileAttribute ,也不需要 java.nio.file.OpenOption

You are not only creating a zero length FileAttribute[] array for the varargs parameter without referring to this type by name, you are also creating a HashSet<OpenOption> without referring to the type OpenOption by name. So this also doesn’t require neither, importing java.nio.file.attribute.FileAttribute nor java.nio.file.OpenOption.

所以底线是,你是否需要 import 不依赖于类型的使用,而是依赖于它的简单名称(并且有多种方法可以使用类型,而不是通过名称引用它)。在第二个示例中,您在方法 printPath(Path passedFilePath)中引用名称 Path ;如果你把它改成 printPath(Object passedFilePath),一切都会再次运作,而没有明确的 import java.nio.file.Path

So the bottom line is, whether you need an import does not depend on the use of the type but whether you refer to it by its simple name (and there are more than one way to use a type without referring to it by name). In your second example you are referring to the name Path in your method printPath(Path passedFilePath); if you change it to printPath(Object passedFilePath), everything will work again without an explicit import of java.nio.file.Path.

这篇关于Java Lambda表达式是否利用“隐藏”?或本地包裹进口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆