当我在构造函数中声明方法时,我收到错误“; expected”。不知道为什么。你能不能帮我理解
public class Reservation {
Date arrivalDate = new Date();
Date departDate = new Date();
Reservation(Date arrDate, Date deptDate)
{
arrivalDate = arrDate;
departDate = deptDate;
long nightsStay();
}
void nightsStay()
{
long days = (departDate.getTime()-arrivalDate.getTime())/(24*60*60*1000);
}
我在我宣布长夜的线路上遇到错误Stay();
答案 0 :(得分:3)
你做不到
long nightsStay();
如果您定义一个变量,您需要一个名称,然后您可以为其指定一个值,但您缺少该名称。
public class Reservation {
Date arrivalDate = new Date();
Date departDate = new Date();
long nightsStay; // add that line
Reservation(Date arrDate, Date deptDate)
{
arrivalDate = arrDate;
departDate = deptDate;
nightsStay = nightsStay(); // change that line
}
long nightsStay()
{
long days = (departDate.getTime()-arrivalDate.getTime())/(24*60*60*1000);
}
}
示例:
long l = 5; //variable l gets value 5;
l = nightsStay(); // calling method nightsStay and storing the result in l;
long nightsStay(); // is neither one
如果您希望方法nightsStay()
返回某些内容,则必须将返回类型从void
更改为您要返回的类型,在您的情况下为long
。< / p>