在方法

时间:2016-03-02 22:27:52

标签: java class variables interface implementation

你好我被指派做以下事情,

  

使用以下方法编写名为DelimitedTextIO的新Java接口:

     
    

o String toText(char delimiter)          - 此方法返回一个包含所有数据的String         将类实现为文本并将每个元素分隔为
        提供了分隔符。

         

o void toObject(扫描仪输入)          - 此方法使用提供的Scanner输入来解析表示实现类数据的分隔文本并初始化
        对象具有已解析值的实例变量。要使用的分隔符         必须在调用此方法之前为扫描仪输入指定。>

  

我无法弄清楚如何创建toObject部分。我遇到的问题是在其他方法中实现它。

import java.util.*;
import java.awt.event.KeyEvent;

public class Explorer extends Occupant{

  private String name;
  private Maze maze;

  public Explorer(Square location, Maze maze, String name){
    super(location);
    this.maze = maze;
    this.name = name;
    maze.lookAround(location);
  }

  public Explorer (Maze maze){
    this.maze = new Maze();
  }

  public String name(){
    return name;
  }

  public void move(int key){
    Square sq = location();

   if(key == KeyEvent.VK_UP || KeyEvent.VK_KP_UP == key){
      if(!sq.wall(Square.UP)){
        this.moveTo(maze.getSquare(sq.row()-1, sq.col()));
      }
    }
    else if(key == KeyEvent.VK_RIGHT || KeyEvent.VK_KP_RIGHT == key){
      if(!sq.wall(Square.RIGHT)){
        this.moveTo(maze.getSquare(sq.row(), sq.col()+1));
      }
    }
    else if(key == KeyEvent.VK_DOWN || KeyEvent.VK_KP_DOWN == key){
      if(!sq.wall(Square.DOWN)){
        this.moveTo(maze.getSquare(sq.row()+1, sq.col()));
      }
    }
    else if(key == KeyEvent.VK_LEFT || KeyEvent.VK_KP_LEFT == key){
      if(!sq.wall(Square.LEFT)){
        this.moveTo(maze.getSquare(sq.row(), sq.col()-1));
      }
    }
  }

  public void moveTo(Square s){
    super.moveTo(s);
    s.enter();
    maze.lookAround(s);
  }

  public void toObject(Scanner input){
    super.toObject(input);
    this.name = input.next();

  }

public String toText(char delimiter){
    String x = this.getClass().getName() + delimiter + location().row()  + delimiter + location().col() + delimiter + name;
    return x;
  }

}

我不确定如何使用toObject方法。如果我做得对,我应该尝试通过迷宫吗?任何帮助表示赞赏。感谢。

0 个答案:

没有答案