Java 子类继承的try catch问题

来源:百度知道 编辑:UC知道 时间:2024/07/16 11:46:05
首先定义了一个类,
public class Vehicule{
private String s;\\车名
private Date da; \\生产日期
public Vehicule(String s,Date da)throws Exception
{this.s=s;
if(da.compareTo(new Date())>0) \\如果da日期在当前日期后,抛出错误
throw new Exception("date incorrect");
this.da=da;
}
````````````}
然后建立一个子类
public class Car extends Vehicule{
private int a; \\车门数
public Car(String e,Date s,int a)throws Exception
{super(e,s);
this.a=a;
}
``````````}
然后调用
public class Application{
public static void main(String[] args) {
Car a;
int s;
String w;
Date ff;\\给出日期
try{
a=new Car(w,ff,s);
}
catch(Exception aaa)
{System.out.println(aaa.getMessage());
System.exit(-1);}
·······}
}
运行之后,无论给出的日期(ff)在当前时间前还是后,最后都会抛出错误“date incorrect”这是为什么呢 ?
谢谢。
要定义一个Date类(2008.12.1)在给Date a=new

这个程序没问题啊,我运行都是对的,你可能传的时间不对吧
需要这样给日期:SimpleDateFormat,它是属于java.text.SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
Date date=null;
date = sdf.parse("2008-12-01");