下面的代码出现在我正在尝试创建的包的主类中.它从名为Journey的帮助程序类引用对象和方法.在journeyCost星号标记的方法中调用方法时,我得到"非静态方法不能从静态上下文引用"错误.这使我感到困惑,因为我认为在第二行创建的Journey对象"thisJourney"构成了类的实例,因此意味着上下文不是静态的.谢谢,Seany.
public boolean journey(int date, int time, int busNumber, int journeyType){
Journey thisJourney = new Journey(date, time, busNumber, journeyType);
if (thisJourney.isInSequence(date, time) == false)
{
return false;
}
else
{
Journey.updateCurrentCharges(date);
thisJourney.costOfJourney = Journey.journeyCost(thisJourney);*****
Journey.dayCharge = Journey.dayCharge + thisJourney.costOfJourney;
Journey.weekCharge = Journey.weekCharge + thisJourney.costOfJourney;
Journey.monthCharge = Journey.monthCharge + thisJourney.costOfJourney;
Balance = Balance - thisJourney.costOfJourney;
jArray.add(thisJourney);
}
}
Run Code Online (Sandbox Code Playgroud)
该错误意味着您尝试以静态方式调用非静态方法,例如:
Journey.journeyCost(thisJourney);
Run Code Online (Sandbox Code Playgroud)
被journeyCost()宣布为静态?你的意思不是thisJourney.journeyCost()吗?
另外,您应该使用getter和setter来修改和访问您的成员变量,而不是:
Journey.dayCharge = ...
Run Code Online (Sandbox Code Playgroud)
你应该有
Journey.setDayCharge(Journey.getDayCharge() + thisJourney.getCostOfJourney());
Run Code Online (Sandbox Code Playgroud)
(setDayCharge和getDayCharge需要是静态在这种情况下)
| 归档时间: |
|
| 查看次数: |
9838 次 |
| 最近记录: |