在 Java 中设置私人 Class

Setting Up a Private Class in Java

这里是新用户。

我一直在为我的面向对象编程 class 编写基于文本的冒险游戏的 "framework" 或草稿。我把它拿给我的助教看,他说它看起来不错,但我应该试着把机芯放在自己的私人空间 class。我为什么要这样做?我应该怎么做?

提前感谢您的帮助。

这是我的主要 class 代码:

public class Main {

public static void main(String args[]) {

    // This is where we will build rooms

    // Load inventory
    ArrayList<String> inventory = new ArrayList<>();

    // Start game
    boolean playing = true;

    while (playing) {

        String input = Input.getInput();

        // Movement commands
        if (input.equals("north")) {
            if (y > 0) {
                y--;
                Rooms.print(room, x, y);
            } else {
                System.out.println("You can't go that way.");
            }
        } else if (input.equals("south")) {
            if (y < HEIGHT - 1) {
                y++;
                Rooms.print(room, x, y);
            } else {
                System.out.println("You can't go that way.");
            }
        } else if (input.equals("east")) {
            if (x > 0) {
                x--;
                Rooms.print(room, x, y);
            } else {
                System.out.println("You can't go that way.");
            }
        } else if (input.equals("west")) {
            if (x < WIDTH - 1) {
                x++;
                Rooms.print(room, x, y);
            } else {
                System.out.println("You can't go that way.");
            }
        }

        // Look commands
        else if (input.equals("look")) {
            Rooms.print(room, x, y);
        }

        // Look at item commands
        else if (input.equals("look thing")) {
            //print item description
        }

        /* Take command
        else if statement

        Drop command
        else if statement

        */

        // Inventory commands
        else if (input.equals("i") || input.equals("inv")
                || input.equals("inventory")) {
            Inventory.print(inventory);
        }

        // Quit commands
        else if (input.equals("quit")) {
            System.out.println("Goodbye!");
            playing = false;

        // Catch-all for invalid input
        } else {
            System.out.println("Invalid input");
        }
    }
    System.exit(0);
}

}

您想要将机芯放在自己的 class 中的一个原因是启用所谓的 Separation of Concerns。遵循这一原则,您希望让您的 class 尽可能与众不同。

随着程序的大小和复杂性的增加,这种方法使 debugging/developing 变得更加容易。

至于将 class 设为私有,我不知道我是否一定同意。我会简单地制作一个 movement class 来处理所有与运动相关的功能和数据。把它放在它自己的文件中,该文件与您的主文件分开,但在您的项目中。

您可以按照相同的方法进行游戏库存、攻击(如果存在)、设置等!

正如我的评论所述: 我可以给你的一个提示是将所有 System.out.println("You can't go that way."); 重构为一个函数并调用它。您重复这一行几次,您可能想稍后更改它,更容易在 1 个函数中完成,而不是查找该字符串的每一次出现。

public static void moveError() {
     System.out.println("You can't go that way.");
}

评论中的一些人建议转换为 switch 语句。我同意这种做法;它不仅更容易看。但也更容易维护。您可以使用作为 strings/integers/characters 传递的特定 'commands' 来表示代码中的特定函数:

switch (direction) {
   case "up":
      // code to move up and error handle
      break;
   case "down":
      // code to move down and error handle
      break;
}

您还可以执行特定的命令,例如拾取物品、调整设置或您设想的任何其他内容。

也许您的助教要求您封装在私有 class 中的房间移动功能与您的外部 class 完全相关,因此如果独立存在则没有任何意义。