Java 更改“a”的值;文件";参数,而不创建类的新实例

Java 更改“a”的值;文件";参数,而不创建类的新实例,java,file,object,parameters,Java,File,Object,Parameters,我这里有个小问题 我在以下方法中声明一个新对象“Fleet”: public void run() throws FileNotFoundException { File file = new File(getFile()); Fleet fleet = new Fleet(file); buildFleet(file, fleet); } private void buildFleet(File file, Fleet fleet) throws FileNo

我这里有个小问题

我在以下方法中声明一个新对象“Fleet”:

public void run() throws FileNotFoundException
{
    File file = new File(getFile());
    Fleet fleet = new Fleet(file);
    buildFleet(file, fleet);
    }

private void buildFleet(File file, Fleet fleet) throws FileNotFoundException
{
    fleet.addVehicle(Honda);
    userMenu(fleet);

}
最后一行调用userMenu()方法。在这个方法中,我需要能够在不创建类的新实例的情况下更改Fleet中“File”的值

private void userMenu(Fleet fleet) throws FileNotFoundException
{
    PrintWriter pw = new PrintWriter("temp.txt");
    File file = new File("temp.txt");
    fleet = new Fleet(file);

    this.createMenu();
    choice = this.menu.getChoice();


while(choice != 8)
{
    switch(choice)
    {
    case 1:
        //Do stuff
        fleet.addVehicle(Honda);
        break;
    }
}
此外,我不允许创建任何新的类级数据。
有什么建议吗?

在您的
车队
类中为文件设置一个setter怎么样:

public class Fleet {
   private File file;
   ...

   public void setFile( File file ){
     this.file = file;
   }
}
然后可以调用此方法,通过调用

fleet.setFile( myNewFile );
已解决:

我改变了:

private void userMenu() throws FileNotFoundException
{
    PrintWriter pw = new PrintWriter("temp.txt");
    File file = new File("temp.txt");
致:

private void userMenu(Fleet fleet) throws FileNotFoundException
{
    PrintWriter pw = new PrintWriter("temp.txt");
    File file = new File("temp.txt");
    fleet.file = file;