有没有办法让这个java变得更小?

有没有办法让这个java变得更小?,java,swing,methods,refactoring,Java,Swing,Methods,Refactoring,我有一个类,在这个类中我有三个方法,它们做同样的事情,但提供不同的输入,所以我想知道是否有任何方法可以使这个调用更小 我的代码 import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.filechooser.FileNameExtensionFilter; public class test { publ

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

我的代码

    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,所有这些都要求用户提供相同的输入,但是每个方法都会从不同的类调用不同的方法

提前谢谢,我希望我已经解释清楚了


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

您可以在方法中提供一个输入开关,使其类似于

 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”,而您需要修改的只是将这些情况添加到开关中。

您可以在方法中提供一个输入开关,使其类似于

 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”,而您需要修改的只是将这些情况添加到开关中。

您可以在方法中提供一个输入开关,使其类似于

 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”,而您需要修改的只是将这些情况添加到开关中。

您可以在方法中提供一个输入开关,使其类似于

 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 Make这样的新函数,然后使用选项从您的三个方法调用它。@user3248466否,请参阅我的编辑,了解调用所需方法的更改。@user3248466我概述的方法以字符作为参数。这意味着你必须确保你的A用单引号括起来,就像我的答案一样。否则编译器会试图找到对变量a的引用-你还没有定义,也不需要定义。Java Devil,现在一切都很好,你的代码真的帮了我很多忙,我忘了在我的主类中有三个按钮调用这三个方法中的每一个。这会影响您提供的代码的行为吗?然后您可以创建一个像@JavaDevil Make这样的新函数,然后使用选项从您的三个方法调用它。@user3248466否,请参阅我的编辑,了解调用所需方法的更改。@user3248466我概述的方法以字符作为参数。这意味着你必须确保你的A用单引号括起来,就像我的答案一样。否则编译器会试图找到对变量a的引用-你还没有定义,也不需要定义。Java Devil,现在一切都很好,你的代码真的帮了我很多忙,我忘了在我的主类中有三个按钮调用这三个方法中的每一个。这会影响您提供的代码的行为吗?然后您可以创建一个像@JavaDevil Make这样的新函数,然后使用选项从您的三个方法调用它。@user3248466否,请参阅我的编辑,了解调用所需方法的更改。@user3248466我概述的方法以字符作为参数。这意味着你必须确保你的A用单引号括起来,就像我的答案一样。否则编译器会试图找到对变量a的引用-你还没有定义,也不需要定义。Java Devil,现在一切都很好,你的代码真的帮了我很多忙,我忘了在我的主类中有三个按钮调用这三个方法中的每一个。这会影响您提供的代码的行为吗?然后您可以创建一个像@JavaDevil Make这样的新函数,然后使用选项从您的三个方法调用它。@user3248466否,请参阅我的编辑,了解调用所需方法的更改。@user3248466我概述的方法以字符作为参数。这意味着你必须确保你的A用单引号括起来,就像我的答案一样。否则编译器会试图找到对变量a的引用-你还没有定义,也不需要定义。Java Devil,现在一切都很好,你的代码真的帮了我很多忙,我现在真的很困惑。基本上,我的主课堂上有三个按钮。buttonA、buttonB和buttonC。buttonA调用methodA,buttonB调用methodB,buttonC调用methodC。但是我不知道我该如何用java恶魔的建议来做到这一点,whi