JAVA编程,小问题,最后第三行代码,谢谢

来源:百度知道 编辑:UC知道 时间:2024/09/28 13:35:59
import java.util.*;
public class TestCalendar
{
public static void main(String[] args)
{
Properties sp=System.getProperties();
Enumeration e=sp.propertyNames();
while(e.hasMoreElements())
{
String key=(String)e.nextElement();
System.out.println(key+"="+sp.getProperty(key));
}
Process p =null;
try
{
p = Runtime.getRuntime().exec("notepad.exe TestProperties.java");
Thread.sleep(5000);
p.destroy();
}
catch(Exception ex){ex.printStackTrace();}

class MyTimerTask extends TimerTask
{
private Timer tm=null;
public void MyTimeTask(Timer tm)
{
this.tm=tm;
}
public void run()
{
try
{
Runtime.getRunti

tm.schedule(new MyTimerTask(tm),30000);
这里的new MyTimerTask()是new 类MyTimerTask的构造方法.而你并没有写它的构造方法,所以是错误的.
public void MyTimeTask(Timer tm)
{
this.tm=tm;
} //这个方法并不是构造方法.
public MyTimeTask(Timer tm)
{
....
} //构造方法应该这样写.

Timer tm = new Timer();
TimerTask mt = new MyTimerTask();
tm.schedule(mt, 30000);

schedule方法的第一个参数是 TimerTask 类型,
虽然MyTimerTask 类继承自它,但是也不能用子类对象