Java 如何扩展在TimerTask中运行的方法。必须接受字符串参数
我有这个密码Java 如何扩展在TimerTask中运行的方法。必须接受字符串参数,java,Java,我有这个密码 private void startActionPerformed(java.awt.event.ActionEvent evt) { this.start.setBackground(Color.green); this.stop.setBackground(Color.lightGray); if (!stopped) { timer.cancel();
private void startActionPerformed(java.awt.event.ActionEvent evt) {
this.start.setBackground(Color.green);
this.stop.setBackground(Color.lightGray);
if (!stopped) {
timer.cancel();
}
stopped = false;
stato.setText("Avviato");
timer = new Timer();
if(giacRitardo>0)
timer.schedule(S.run("argiacenze"), giacRitardo, giacRitardo);//parti dopo x secondi e itera ogni x secondi
if(cliRitardo>0)
timer.schedule(S.run("arclienti"), cliRitardo, cliRitardo);//parti dopo x secondi e itera ogni x secondi
//其他代码
及
及
我需要扩展上面运行的方法,以便向其传递字符串参数。
a怎么能做到呢。
我几乎是java的新手。所以,请原谅我缺乏经验
实际上我收到了错误:
找不到适合运行的方法(字符串)
方法TimerTask.run()不适用
(实际参数列表和正式参数列表长度不同)
方法MainForm.TaskSchedulato.run()不适用
(实际参数列表和正式参数列表长度不同)
提前感谢您这可以很容易地用实现。 简言之,方法重载是一种语言特性,它允许声明具有相同名称但参数不同的多个方法 应用于您的问题,而不是覆盖父级的
run()
方法,只需声明另一个run()
方法,如下所示:
public void run(String someInput) {
/* ... */
}
当然,如果在程序中有意义,可以从run(String)
中调用run()
:
public void run(String someInput) {
/* Do something with someInput */
run(); // Hand over to parent's run() method
/* Maybe do some other stuff */
}
根据您尝试执行的操作,您可能希望同时使用方法重载和重写。需要更多的上下文来给出更具体的建议。@domdom,
时间表上的签名是
计划(TimerTask任务、长延迟、长周期)
我已经在我的工作版本的程序中使用了它
private void startActionPerformed(java.awt.event.ActionEvent evt) {
this.start.setBackground(Color.green);
this.stop.setBackground(Color.lightGray);
if (!stopped) {
timer.cancel();
}
stopped = false;
stato.setText("Avviato");
timer = new Timer();
if(giacRitardo>0)
timer.schedule(new TaskGiacenze(), giacRitardo, giacRitardo);
if(cliRitardo>0)
timer.schedule(new TaskClienti(), cliRitardo, cliRitardo);
if(artRitardo>0)
timer.schedule(new TaskArticoli(), artRitardo, artRitardo
//..........
及
但我必须为每种类型创建一个类(一个类用于giacenze,另一个类用于Artioli等等),而不是将其作为参数调用。我这样解决了这个问题
class TaskSchedulato extends TimerTask {
String stringa;
public TaskSchedulato(String stringa){
this.stringa=stringa;
}
@Override
public void run() {
//code here
}
感谢@onurbaysan在下面的帖子中给出了答案
谢谢你,多姆,谢谢@多姆。我超载了。现在,当我调用method作为timer.schedule中的第一个参数时,我收到一个“'void'type not allowed here”错误。Nicola,您的
run()
方法不返回任何内容(void
)。您正在将run()
的返回值作为第一个参数传递给timer.schedule()
方法。但是,方法不能接受类型为void
的参数。timer.schedule()
的签名是什么?它期望的第一个参数是什么?可能的
private void startActionPerformed(java.awt.event.ActionEvent evt) {
this.start.setBackground(Color.green);
this.stop.setBackground(Color.lightGray);
if (!stopped) {
timer.cancel();
}
stopped = false;
stato.setText("Avviato");
timer = new Timer();
if(giacRitardo>0)
timer.schedule(new TaskGiacenze(), giacRitardo, giacRitardo);
if(cliRitardo>0)
timer.schedule(new TaskClienti(), cliRitardo, cliRitardo);
if(artRitardo>0)
timer.schedule(new TaskArticoli(), artRitardo, artRitardo
//..........
class TaskGiacenze extends TimerTask {
@Override
public void run() {
redirectSystemStreams();
DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
URL sito = null;
try {
sito = new URL(sUrl + "?aggiornamento=argiacenze");
} catch (MalformedURLException ex) {
System.out.println("Indirizzo del sito mal formato o inesistente");
}
URLConnection yc = null;
try {
yc = sito.openConnection();
} catch (IOException ex) {
System.out.println("Errore di connessione _ ");
}
BufferedReader in = null;
try {
in = new BufferedReader(
new InputStreamReader(
yc.getInputStream()));
} catch (IOException ex) {
Logger.getLogger(TaskGiacenze.class.getName()).log(Level.SEVERE, null, ex);
}
String inputLine;
try {
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
} catch (IOException ex) {
Logger.getLogger(TaskGiacenze.class.getName()).log(Level.SEVERE, null, ex);
dataErrore = new Date();
System.out.println(sdf.format(dataErrore));
System.out.println("Errore di connessione: " + dataErrore);
}
try {
in.close();
} catch (IOException ex) {
Logger.getLogger(TaskGiacenze.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class TaskSchedulato extends TimerTask {
String stringa;
public TaskSchedulato(String stringa){
this.stringa=stringa;
}
@Override
public void run() {
//code here
}