为什么java接口会返回自身<;java.nio.file.Path>;
为什么java接口会返回自身 我正在读当前代码为什么java接口会返回自身<;java.nio.file.Path>;,java,interface,Java,Interface,为什么java接口会返回自身 我正在读当前代码 public class Find { public static class Finder extends SimpleFileVisitor<Path>{ private final PathMatcher matcher; private int numMatches = 0; Finder(String pattern){ matcher = FileSystems.getDefault
public class Find {
public static class Finder extends SimpleFileVisitor<Path>{
private final PathMatcher matcher;
private int numMatches = 0;
Finder(String pattern){
matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
}
void find(Path file){
Path name = file.getFileName();
System.out.println("Name " + name);
if (name != null && matcher.matches(name)){
numMatches++;
System.out.println(file);
}
}
void done(){
System.out.println("Matched: " + numMatches);
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs){
find(file);
return CONTINUE;
}
它必须是字符串,因为它是可打印的 但是看看界面
public interface Path
extends Comparable<Path>, Iterable<Path>, Watchable
/**
* Returns the name of the file or directory denoted by this path as a
* {@code Path} object. The file name is the <em>farthest</em> element from
* the root in the directory hierarchy.
*
* @return a path representing the name of the file or directory, or
* {@code null} if this path has zero elements
*/
Path getFileName();
/**
*返回此路径表示为
*{@code Path}对象。文件名是距离最远的元素
*目录层次结构中的根目录。
*
*@返回表示文件或目录名称的路径,或
*{@code null}如果此路径有零个元素
*/
路径getFileName();
它返回一个路径对象,为什么这样做?
+
当与至少一个String类型的操作数一起使用时,
是String
串联运算符。你需要研究一下这个方法
美国
+运算符广泛用于打印语句。例如:
String string1 = "saw I was ";
System.out.println("Dot " + string1 + "Tod");
哪张照片
Dot saw I was Tod
这样的串联可以是任何
物体。对于每个不是字符串的对象,其toString()方法
调用以将其转换为字符串
基本上,所有引用类型都继承
对象
类,因此继承toString()
方法。当您将字符串
与不同类型的引用连接起来时,将调用该类型的toString()
方法,并连接产生的字符串。此语句为false:
它必须是字符串,因为它是可打印的
当使用变量代替字符串时,Java编译器将隐式调用对象的toString()
方法(所有对象都有该方法)。对于将输出路径的字符串表示形式的Path
。打印文本并不意味着它是一个实际的String
实例。Path getFileName()返回Path对象,所以每当我们打印对象时,就会调用它的toString()方法,该方法从Object类继承而来
因此,将调用Path的toString()方法,并以字符串形式返回Path对象
因此,请执行类似于path.getFileName().getName()的操作如果有这样的方法,则使用code>获取名称。“它必须是字符串,因为它是可打印的。”这不符合要求。由于toString()方法,Java中的每个对象都是可打印的。
Dot saw I was Tod