我有这段代码
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
//其他一些代码
和
class TaskSchedulato 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=arlingue");
} 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(TaskSchedulato.class.getName()).log(Level.SEVERE, null, ex);
}
String inputLine;
try {
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
} catch (IOException ex) {
Logger.getLogger(TaskSchedulato.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(TaskSchedulato.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
和
TaskSchedulato S = new TaskSchedulato();
我需要扩展上面运行的方法,以便我可以传递一个字符串参数。 怎么做呢。 我几乎是java中的新星。所以,请原谅我缺乏经验。
其实我收到错误: 找不到合适的运行方法(String) 方法TimerTask.run()不适用 (实际和正式的参数列表长度不同) 方法MainForm.TaskSchedulato.run()不适用 (实际和正式的参数列表长度不同)
提前谢谢
答案 0 :(得分:3)
使用method overloading可以轻松实现这一目标。
简而言之,方法重载是一种语言功能,它允许声明具有相同名称但参数不同的多个方法。
应用于您的问题,而不是覆盖父{q} run()
方法,只需声明另一个run()
方法,如下所示:
public void run(String someInput) {
/* ... */
}
当然,如果您的计划有意义,可以在run()
内拨打run(String)
:
public void run(String someInput) {
/* Do something with someInput */
run(); // Hand over to parent's run() method
/* Maybe do some other stuff */
}
根据您要执行的操作,您可能希望同时使用方法重载和覆盖。需要更多的背景来提供更具体的建议。
答案 1 :(得分:1)
I solved this way
class TaskSchedulato extends TimerTask {
String stringa;
public TaskSchedulato(String stringa){
this.stringa=stringa;
}
@Override
public void run() {
//code here
}
Thanks to @onurbaysan for the answer in the thread below How to Pass Arguments to Timertask Run Method
答案 2 :(得分:0)
@domdom, timer.schedule的签名是 schedule(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
//..........
和
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);
}
}
}
但是我必须为每种类创建一个类(giacenze的一个类,articoli的另一个类等等......)而不是将其作为参数调用。