Java 如何创建文件字段?

Java 如何创建文件字段?,java,file,Java,File,我有一个将文件作为参数的构造函数。如何为文件创建一个字段,以便所有其他方法都可以访问它 构造函数将如下所示: MyClass(File f) throws Exception; . . . . . . . . . . . x . . . . x x . . . . . . . . . . . . public class MyClass { private File file; public MyClass(File f) throws Exception {

我有一个将文件作为参数的构造函数。如何为文件创建一个字段,以便所有其他方法都可以访问它

构造函数将如下所示:

MyClass(File f) throws Exception;
. . . . .
. . . . .
. x . . .
. x x . .
. . . . .
. . . . .
public class MyClass {
    private File file;

    public MyClass(File f) throws Exception {
        this.file = f;
    }
}
这个文件看起来像这样:

MyClass(File f) throws Exception;
. . . . .
. . . . .
. x . . .
. x x . .
. . . . .
. . . . .
public class MyClass {
    private File file;

    public MyClass(File f) throws Exception {
        this.file = f;
    }
}

我想创建一个从这个文件中获取行和列的方法,但要做到这一点,我需要一个字段

向类中添加成员,并将所需信息从文件对象复制到类中

您还可以简单地向类添加一个文件成员,在构造函数中设置引用,如下所示:

MyClass(File f) throws Exception;
. . . . .
. . . . .
. x . . .
. x x . .
. . . . .
. . . . .
public class MyClass {
    private File file;

    public MyClass(File f) throws Exception {
        this.file = f;
    }
}

顺便说一句:因为这是Java的基础知识,你应该真正尝试学习基本的东西。Java在线教程数以千计。

请参阅。或者只需阅读任何java手册/指南。