请解释下面显示的一行代码。

huangapple go评论68阅读模式
英文:

Please explain a line of code shown below

问题

这段代码不是我自己的。来源:Java Swing - JFileChooser示例

JFileChooser jfc = new JFileChooser(
    FileSystemView.getFileSystemView().getHomeDirectory());

请解释这行代码。我知道这是方法链,但FileSystem是一个对象,对吗?

所以,JFileChooser对象将带有两个链接方法的FileSystemView对象作为参数?

getFileSystemView()方法是否调用了getHomeDirectory()方法?

FileChooser1

package com.mkyong.jfileChooser;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

public class FileChooser1 {

    public static void main(String[] args) {
        JFileChooser jfc = new JFileChooser(
            FileSystemView.getFileSystemView().getHomeDirectory());//<--p

        int returnValue = jfc.showOpenDialog(null);
        // int returnValue = jfc.showSaveDialog(null);

        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File selectedFile = jfc.getSelectedFile();
            System.out.println(selectedFile.getAbsolutePath());
        }
    }
}
英文:

This code is not my own. Credit: Java Swing – JFileChooser example

JFileChooser jfc = new JFileChooser(
    FileSystemView.getFileSystemView().getHomeDirectory());

Please explain this line of code. I get that it's method chaining, but FileSystem is on object correct?

So, the JFileChooser object is taking the FileSystemView object with the two chained methods as it's arguments?

Is the getFileSystemView() method calling the getHomeDirectory() method?

FileChooser1

package com.mkyong.jfileChooser;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

public class FileChooser1 {

	public static void main(String[] args) {
		JFileChooser jfc = new JFileChooser(
            FileSystemView.getFileSystemView().getHomeDirectory());//<--p

		int returnValue = jfc.showOpenDialog(null);
		// int returnValue = jfc.showSaveDialog(null);

		if (returnValue == JFileChooser.APPROVE_OPTION) {
			File selectedFile = jfc.getSelectedFile();
			System.out.println(selectedFile.getAbsolutePath());
		}
	}
}

答案1

得分: 1

JFileChooser类有多种类型的构造函数定义(构造函数重载)。
其中一个构造方法是JFileChooser(File),它接受一个File对象值作为参数。

在您的代码中,JFileChooser实例是使用一个代表主目录的File对象实例创建的。

FileSystemView是一个类,具有一个名为getFileSystemView的静态方法,用于返回一个FileSystemView实例(因此称为工厂方法,类似于生成某个产品的工厂)。
FileSystemView类型的对象中有一个名为getHomeDirectory()的方法,它将返回一个File对象,该对象是主目录的抽象表示。
总结下面这行代码:

JFileChooser jfc = new JFileChooser(
    FileSystemView.getFileSystemView().getHomeDirectory());

您正在使用构造函数类型JFileChooser(File)实例化一个JFileChooser实例,而不是显式传递一个File值,而是调用FileSystemView方法来返回一个值,该值将作为参数传递给JFileChooser构造函数。

相同的代码可以编写如下:

FileSystemView fileSystemView = FileSystemView.getFileSystemView();
File file = fileSystemView.getHomeDirectory();
JFileChooser jfc = new JFileChooser(file);//<--p
英文:

The JFileChooser class has multiple types of constructors defined(Constructor Overloading).
One of the constructor method is JFileChooser(File) which takes a File object value as the argument.

In your code,the JFileChooser instance is being created with a File object instance represnting the homedirectory as the parameter.

The FileSystemView is a class with getFileSystemView static method to return an instance of the FileSystemView(Hence called a factory method,analogous to a factory which produces some product).
The FileSystemView type object has a method in it called getHomeDirectory() which will return the a File object which is an abstraction of the homedirectory.
To summarize the below line of code:

    JFileChooser jfc = new JFileChooser(
        FileSystemView.getFileSystemView().getHomeDirectory());

you are instantiating an instance of JFileChooser using the constructor type JFileChooser(File) and rather than passing a File value explicitly you are invoking the FileSystemView method to return the value which will be passed as an argument to the JFileChooser constructor.

The same code can be written as below

    FileSystemView fileSystemView = FileSystemView.getFileSystemView();
    File file = fileSystemView.getHomeDirectory();
    JFileChooser jfc = new JFileChooser(file);//&lt;--p

huangapple
  • 本文由 发表于 2020年8月23日 11:01:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63542983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定