简体   繁体   English

将文件作为参数传递时出现问题

[英]Issue with passing in a File as a parameter

I have a class ArrayListDVDCollection that is extended from my application class file DVDApplication . 我有一个从我的应用程序类文件DVDApplication扩展的ArrayListDVDCollection类。 The ArrayListDVDCollection class implements an interface with just the method declaration for loadData . ArrayListDVDCollection类仅使用loadData的方法声明实现接口。 Inside of my application file where I will run the actual program, I wanted to make a File object that would be able to be passed into the loadData method. 在运行实际程序的应用程序文件内部,我想制作一个File对象,该对象可以传递到loadData方法中。 However, it keeps giving me errors and stating that the method itself needs to be static. 但是,它不断给我带来错误,并指出该方法本身必须是静态的。 How can I successfully pass in a file into my loadData method in the application file? 如何将文件成功传递到应用程序文件的loadData方法中?

public class ArrayListDVDCollection implements DVDCollectionInterface
{  
    public ArrayList<DVD> loadData(File dvdData){
        try{
            BufferedReader kbd = new BufferedReader(new FileReader(dvdData)); // Open the DVDCollection file.
            String line;
            while ((line = kbd.readLine()) != null) // Read the contents.
            {
                String dvdTitle = line;
                String dvdCategory = kbd.readLine();
                String dvdRunningTime = kbd.readLine();
                int dvdYear = Integer.parseInt(kbd.readLine());
                double dvdPrice = Double.parseDouble(kbd.readLine());

                DVDArrayList.add(new DVD (dvdTitle, dvdCategory, dvdRunningTime, dvdYear, dvdPrice));
            }
            kbd.close(); // Close file after reading
        }catch (Exception e){
            System.out.println("Error reading file.");
        }
        return DVDArrayList;
    }
}

Application file 申请文件

public class DVDApplication extends ArrayListDVDCollection{
    public static void main(String[] args){
        ArrayList<DVD> DVDArrayList = new ArrayList<DVD>();
        File dvdData = new File("DVDCollection.txt");
        DVDArrayList = loadData(dvdData);
    }
} 
public class DVDApplication extends ArrayListDVDCollection{
public static void main(String[] args){
    ArrayList<DVD> DVDArrayList = new ArrayList<DVD>();
    File dvdData = new File("DVDCollection.txt");
    DVDArrayList = new ArrayListDVDCollection ().loadData(dvdData);
}

} }

because loadData is non-static method and you need to call it via reference of ArrayListDVDCollection . 因为loadData是非静态方法,所以您需要通过ArrayListDVDCollection的引用来调用它。

The quick and dirty solution is: 快速而肮脏的解决方案是:

public class DVDApplication extends ArrayListDVDCollection{
    public static void main (String[] args){
        ArrayList<DVD> DVDArrayList = new ArrayList<DVD>();
        File dvdData = new File ("DVDCollection.txt");
        DVDApplication dvdapp = new DVDApplication ();
        DVDArrayList = dvdapp.loadData (dvdData);
    }
} 

Since the method loadData isn't static, you need an Instance, to call the method on it. 由于方法loadData不是静态的,因此需要一个实例来在其上调用该方法。

Since the parent class, you're using, doesn't access member variables, an alternative approach would be, to make the method static there: 由于您正在使用的父类不访问成员变量,因此可以采用另一种方法使该方法在此处静态化:

public class ArrayListDVDCollection implements DVDCollectionInterface
{  
    public static ArrayList<DVD> loadData(File dvdData) {

Then you could have left your main method unchanged. 然后,您可以保留主要方法不变。

There is strong connection between having member variables and methods, depending on their state, and the methods being static or not, and calling them in a static way and static context. 在具有成员变量和方法(取决于它们的状态)与方法是否静态之间,以及以静态方式和静态上下文调用它们之间,存在紧密的联系。

In this case, only the keywords didn't reflect the independence from any mutable state. 在这种情况下,只有关键字不能反映与任何可变状态的独立性。 Maybe this could have been solved otherwise by Java, like automatic dependency checking without static keywords - maybe it would be to costly. 也许用Java可以解决此问题,例如不使用静态关键字的自动依赖项检查-可能会很昂贵。 Surely it is a way of documentation. 当然,这是一种记录方法。

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

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