0

在我正在进行的番茄钟应用程序的主要方法中,我有以下代码:

    ActionListener a9 = new ActionListener(){
        public void actionPerformed(ActionEvent evt){

        DateFormat dateFormat = new SimpleDateFormat("HH:mm");
        String currentTime = (String) dateFormat.format(new Date());

        combo2.append("You completed " + i + ""
        +  " pomodoros! At " + currentTime + " \n");
         Toolkit.getDefaultToolkit().beep();
        }
      };


    Timer newTimer = new Timer(500, a9);
    newTimer.start();
    newTimer.setRepeats(false);

但是当我使用 eclipse 的“提取方法”功能时,我得到了这一部分:

private static void generatePomodoro(final JTextArea combo2) {
    ActionListener a9 = new ActionListener(){
        public void actionPerformed(ActionEvent evt){

        DateFormat dateFormat = new SimpleDateFormat("HH:mm");
        String currentTime = (String) dateFormat.format(new Date());

        combo2.append("You completed " + i + ""
        +  " pomodoros! At " + currentTime + " \n");
         Toolkit.getDefaultToolkit().beep();
        }
      };

    Timer newTimer = new Timer(500, a9);
    newTimer.start();
    newTimer.setRepeats(false);
}

这并没有达到我的预期(让我连续运行倒数计时器)。& 对我来说看起来真的很神秘......签名是从哪里来的?

private static void generatePomodoro(final JTextArea combo2)

谢谢

4

1 回答 1

0

您的main方法具有签名public static void main(String[] args),当您尝试使用时Extract Method,Eclipse 必须保留新方法static,因此可以从中访问它main。新函数也是private因为重构之前的代码无论如何都无法从外部访问,并且保持函数私有是一种好习惯。

于 2013-12-18T13:06:30.550 回答