javaoop优化代码
我正在做我的Java作业——扫雷游戏克隆。我有两个几乎相同的(只有文本标签和文本框不同)方法gameWon()和gameLost(),它们负责在游戏结束时显示“Game Won!”/“Game Lost”窗口。我知道代码复制是一种不好的做法,所以我想对其进行优化。问题是我对OOP有点陌生,我真的不知道怎么做。也许我可以将这些方法合并成一个在不同情况下采取不同行动的方法,或者继承可能会有用。我真的不知道,希望你们中的一些人能帮我一点忙。谢谢你的回答 以下是这些方法的代码: gameOverjavaoop优化代码,java,oop,inheritance,polymorphism,code-duplication,Java,Oop,Inheritance,Polymorphism,Code Duplication,我正在做我的Java作业——扫雷游戏克隆。我有两个几乎相同的(只有文本标签和文本框不同)方法gameWon()和gameLost(),它们负责在游戏结束时显示“Game Won!”/“Game Lost”窗口。我知道代码复制是一种不好的做法,所以我想对其进行优化。问题是我对OOP有点陌生,我真的不知道怎么做。也许我可以将这些方法合并成一个在不同情况下采取不同行动的方法,或者继承可能会有用。我真的不知道,希望你们中的一些人能帮我一点忙。谢谢你的回答 以下是这些方法的代码: gameOver pub
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);
}
您应该只有一个方法,称之为
gameOver(..)
,乍一看,您只需要两个参数,title
和message
。然后,只更改两行代码:
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!");
您可以做的最简单的事情是将标题和消息的字符串作为参数,或者通过使用一个布尔参数来表示游戏是否获胜,并在设置字符串的方法中进行布尔测试,例如:
public static void gameOver(boolean won) {
....
F1 = new JFrame(won?"Game Won":"Game Over");
....
}
仔细看一下代码,仅仅按照其他答案中的建议传递布尔或字符串参数是不够的。您必须做的工作是识别通用和不同的代码(这里有两种)方法。在你的情况下,我会想到:
- 头衔
- 信息
- 按钮1消息
- 按钮1侦听器
- 按钮2消息
- 按钮2侦听器
public static void showTwoButtonMessage(String title, String message, String button1Message, ActionListener listener1, String button2Message, ActionListener listener2){ //... }
因此,您有了一个简洁的小方法,可以重复使用来显示任何双按钮窗口。您可以创建一个通用的
GameComplete
版本,其中包含两个字符串。这将允许您重用相同的代码并显示不同的文本
public static void showTwoButtonMessage(String title, String message,
String button1Message, ActionListener listener1,
String button2Message, ActionListener listener2){
//...
}