【问题标题】:Java OOP optimising codeJava OOP 优化代码
【发布时间】:2013-05-27 22:26:20
【问题描述】:

我正在处理我的 Java 作业 - Minesweeper 游戏克隆。我有两个几乎相同的(只有文本标签和文本框架不同)方法 gameWon() 和 gameLost() 负责在游戏结束时显示“Game Won!”/“Game Lost”窗口。我知道代码重复是不好的做法,所以我想对其进行优化。问题是我对 OOP 有点陌生,我不确定该怎么做。也许我可以以某种方式将这些方法合并为一个,以便在不同情况下采取不同的行动,或者继承会很有用。我真的不知道,希望你们中的一些人能帮助我一点。感谢您的回答。

以下是这些方法的代码:

游戏结束

public static void gameOver() {

        F1 = new JFrame("Game Over"); 
        F1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

        F1.setSize(360, 120);
        Container content = F1.getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout()); 

        JLabel textLabel = new JLabel("Sorry, you have lost this game! Better luck next time.",SwingConstants.CENTER); 
        textLabel.setPreferredSize(new Dimension(360, 40));
        content.add(textLabel, BorderLayout.CENTER);

        JButton button = new JButton("Exit");
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                System.exit(0);
            }
        }); 
        content.add(button);

        button = new JButton("Restart This Game");  
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                F1.dispose();
                Board.doRepaint();
            }
        });         
        content.add(button);

        button = new JButton("Play Again"); 
        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                F1.dispose();
                restartGame();
            }
        });         
        content.add(button);

        F1.setLocationRelativeTo(null);
        F1.setVisible(true); 
    }

gameWon

public static void gameWon() {  
   F1 = new JFrame("Game Won"); 
   F1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

   F1.setSize(360, 120);
   Container content = F1.getContentPane();
   content.setBackground(Color.white);
   content.setLayout(new FlowLayout()); 

   JLabel textLabel = new JLabel("Congratulations, you have won the game!",SwingConstants.CENTER); 
   textLabel.setPreferredSize(new Dimension(360, 40));
   content.add(textLabel, BorderLayout.CENTER);

   JButton button = new JButton("Exit");
   button.addActionListener(new ActionListener() {

     public void actionPerformed(ActionEvent e)
     {
        System.exit(0);
     }
  }); 
  content.add(button);

  button = new JButton("Restart This Game");    
  button.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {
        F1.dispose();
        Board.doRepaint();
    }
  });       
  content.add(button);

  button = new JButton("Play Again");   
  button.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {
        F1.dispose();
        restartGame();
    }
  });       
  content.add(button);

  F1.setLocationRelativeTo(null);
  F1.setVisible(true); 
}

【问题讨论】:

  • 你可以创建一个通用的GameComplete 版本,它接受几个字符串。这将允许您重用相同的代码并显示不同的文本

标签: java oop inheritance polymorphism code-duplication


【解决方案1】:

你应该只有一个方法,叫它gameOver(....),乍一看,你只需要两个参数,titlemessage。然后,只修改两行代码:

public static void gameOver(final String title, final String message) {
  .....
  F1 = new JFrame(title);
  .....
  JLabel textLabel = new JLabel(message ,SwingConstants.CENTER);
}

然后,不要调用两个方法,而是使用不同的参数调用同一个方法:

gameOver("Game Won", "Congratulations, you have won the game!");

【讨论】:

    【解决方案2】:

    您可以做的最简单的事情是将标题和消息的字符串作为参数,或者通过一个布尔参数来表示游戏是否已获胜,并在设置字符串的方法中进行布尔测试,例如:

    public static void gameOver(boolean won) {
        ....
        F1 = new JFrame(won?"Game Won":"Game Over");
        ....
    }
    

    【讨论】:

      【解决方案3】:
      public static void gameEnd(boolean hasWon) {
      
          String title = hasWon ? "Game Won" : "Game Over";
          F1 = new JFrame(title); 
          F1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      
          F1.setSize(360, 120);
          Container content = F1.getContentPane();
          content.setBackground(Color.white);
          content.setLayout(new FlowLayout()); 
      
          String message = hasWon ? "Congratulations, you have won the game!" :
                "Sorry, you have lost this game! Better luck next time.";
          JLabel textLabel = new JLabel(message,SwingConstants.CENTER);
          textLabel.setPreferredSize(new Dimension(360, 40));
          content.add(textLabel, BorderLayout.CENTER);
      
          JButton button = new JButton("Exit");
          button.addActionListener(new ActionListener() {
      
              public void actionPerformed(ActionEvent e)
              {
                  System.exit(0);
              }
          }); 
          content.add(button);
      
          button = new JButton("Restart This Game");  
          button.addActionListener(new ActionListener() {
      
              public void actionPerformed(ActionEvent e)
              {
                  F1.dispose();
                  Board.doRepaint();
              }
          });         
          content.add(button);
      
          button = new JButton("Play Again"); 
          button.addActionListener(new ActionListener() {
      
              public void actionPerformed(ActionEvent e)
              {
                  F1.dispose();
                  restartGame();
              }
          });         
          content.add(button);
      
          F1.setLocationRelativeTo(null);
          F1.setVisible(true); 
      }
      

      【讨论】:

        【解决方案4】:

        更仔细地查看代码,仅按照其他答案中的建议传递布尔值或字符串参数是不够的。您必须做的工作是识别所有(这里:两个)方法的共同代码和不同代码。在你的情况下,我会想出这个:

        • 标题
        • 留言
        • 按钮 1 消息
        • 按钮 1 监听器
        • 按钮 2 消息
        • 按钮 2 监听器

          public static void showTwoButtonMessage(String title, String message,
           String button1Message, ActionListener listener1,
           String button2Message, ActionListener listener2){
          //...
          }
          

        这样你就有了一个简洁的小方法,你可以重复使用来显示任何两个按钮的窗口。

        【讨论】:

          猜你喜欢
          • 2010-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多