【问题标题】:Is there any way to make this java called smaller?有什么办法可以让这个 java 变小吗?
【发布时间】:2014-01-29 19:37:50
【问题描述】:

我有一个类,在类中我有三个方法,它们做同样的事情但提供不同的输入,所以我想知道是否有任何方法可以使这个调用更小。

我的代码;

    import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

public class test {

    public void methodclassA() {

        int result = JOptionPane
                .showOptionDialog(
                        null,
                        "How would you like you insert your data, manually or from a file? ",
                        "Inserting data", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, new String[] {
                                "Manual", "From a File" },
                        JOptionPane.NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {

            // Going to call methodA from another class
        }

        if (result == JOptionPane.NO_OPTION) {

            JTextField NameField = new JTextField();
            Object[] message = { "Path location:", NameField };

            int result2 = JOptionPane.showOptionDialog(null, message,
                    "Inserting data", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok",
                            "Locate the file" }, JOptionPane.NO_OPTION);
        }
    }

    public void methodclassB() {

        int result = JOptionPane
                .showOptionDialog(
                        null,
                        "How would you like you insert your data, manually or from a file? ",
                        "Inserting data", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, new String[] {
                                "Manual", "From a File" },
                        JOptionPane.NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {

            // Going to call methodB from another class
        }

        if (result == JOptionPane.NO_OPTION) {

            JTextField NameField = new JTextField();
            Object[] message = { "Path location:", NameField };

            int result2 = JOptionPane.showOptionDialog(null, message,
                    "Inserting data", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok",
                            "Locate the file" }, JOptionPane.NO_OPTION);
        }
    }

    public void methodclassC() {

        int result = JOptionPane
                .showOptionDialog(
                        null,
                        "How would you like you insert your data, manually or from a file? ",
                        "Inserting data", JOptionPane.YES_NO_OPTION,
                        JOptionPane.QUESTION_MESSAGE, null, new String[] {
                                "Manual", "From a File" },
                        JOptionPane.NO_OPTION);
        if (result == JOptionPane.YES_OPTION) {

            // Going to call methodB from another class
        }

        if (result == JOptionPane.NO_OPTION) {

            JTextField NameField = new JTextField();
            Object[] message = { "Path location:", NameField };

            int result2 = JOptionPane.showOptionDialog(null, message,
                    "Inserting data", JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok",
                            "Locate the file" }, JOptionPane.NO_OPTION);
        }
    }

}

例如,我在课堂上的三个方法是; methodclassA、methodclassB、methodclassC,所有这些都要求用户输入相同的输入,但是每个方法都会从不同的类调用不同的方法。

提前谢谢,我希望我已经清楚地解释了自己。

edit:我之前忘了提,我的主类中有三个按钮,它们分别调用这三个方法。比如我的buttonA调用methodclassA,buttonB调用methodclassB,buttonC调用methodclassC。

【问题讨论】:

  • 你能先格式化你的代码吗?

标签: java swing methods refactoring


【解决方案1】:

您可以只在方法中提供一个输入开关,这样它就会像

 public void methodCaller(char aSwitcher) {
      int result = JOptionPane.showOptionDialog(null, "How would you like you insert your data, manually or from a file? ", "Inserting data", 
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Manual", "From a File" }, JOptionPane.NO_OPTION);
      if (result == JOptionPane.YES_OPTION) {
           switch(aSwitcher)
           {
               case 'A':
                   //Going to call methodA from another class
                   break;
               case 'B':
                   //Going to call methodB from another class
                   break;
               case 'C':
                   //Going to call methodC from another class
                   break;
               default:
                   throw new IllegalArgumentExcpetion("No method defined for option" + aSwitcher);
           }


      }
      else if (result == JOptionPane.NO_OPTION) {
           JTextField NameField = new JTextField(); 
           Object[] message = {"Path location:", NameField};
           int result2 = JOptionPane.showOptionDialog(null, message, "Inserting data", 
                     JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, new String[] { "Ok", "Locate the file" }, JOptionPane.NO_OPTION);
      }
 }

可能是一种更好的方法,但这至少可以节省所有代码重复。

然后调用只是做这些替换

methodclassA();  -> methodCaller('A');
methodclassB();  -> methodCaller('B');
methodclassC();  -> methodCaller('C');

这样做的另一个好处是您可以添加“D”、“E”、“F”,而您只需将这些情况添加到开关即可。

【讨论】:

  • 您好,在我的主类中有三个按钮调用这三个方法之前,我忘了提及。这会影响您提供的代码的行为吗?
  • 然后你可以像@JavaDevil 做的那样创建一个新函数,然后从你的三个方法中调用它。
  • @user3248466 不,请参阅我对调用所需方法的更改的编辑。
  • @user3248466 我概述的方法将 char 作为参数。这意味着您必须确保您的 A 用单引号括起来,就像我的回答一样。否则编译器将尝试查找对变量 A 的引用 - 您尚未定义,也不需要定义。
  • Java Devil,现在一切正常,你的代码对我帮助很大
【解决方案2】:

我知道它们现在都在同一个类中,但根据它们的调用方式,您可以重构并使用 Template Method Design Pattern

【讨论】:

  • 嗨,我现在真的很困惑。基本上,我的主要课程中有三个按钮。按钮 A、按钮 B 和按钮 C。按钮A调用方法A,按钮B调用方法B,按钮C调用方法C。但是我不知道我应该如何使用 java devil 的建议来做到这一点,我喜欢并且更容易理解。
猜你喜欢
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 2020-03-05
  • 2020-04-23
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
相关资源
最近更新 更多