我正在编写一个基于注册家中电器正在使用的能量消耗量的程序。到目前为止,我已经创建了各种仪表类,例如WaterMeter,GasMeter等,其中需要使用空值方法存储值,我还创建了具有用于记录每个电器内能量消耗的方法的电器类。我现在正在处理的是应用存储在构造函数中的能量值,将这些值放入一个timePasses()
方法中,然后这些方法将这些值返回到其特定仪表的方法,以便它们可以注册。这是我到目前为止有:如何构造一个需要从构造函数中传递值的方法?
电器类的例子:
public class ElectricShower extends Shower
{
public int isOn = -1;
public int isOff = 0;
public int incrementTime;
public int x = -1;
private static ElectricMeter instance = new ElectricMeter();
public static ElectricMeter getInstance() { return instance; }
@Override
public int currentState()
{
if (x == 0)
return isOff;
else
{
return isOn;
}
//returns isOn;
}
@Override
public void useTime(int defaultTime)
{
defaultTime = 15;
incrementTime = 1;
}
public void shower()
{
//call timePasses() method
}
@Override
public int timePasses()
{
if(x == isOff)
return 0;
else
{
ElectricMeter.getInstance().incrementConsumed(electricityUse);
}
}
ElectricShower(int electricityUse, int gasUse, int waterUse, int timeOn)
{
super(electricityUse, gasUse, waterUse, timeOn);
this.electricityUse = 12 * incrementTime;
this.gasUse = 0 * incrementTime;
this.waterUse = 4 * incrementTime;
this.timeOn = 15 * incrementTime;
}
}
表例如:
public class ElectricMeter
{
public int incrementConsumed(int value)
{
}
public int incrementGenerated()
{
}
public boolean canGenerate()
{
}
public String getConsumed()
{
}
public String getGenerated()
{
}
}
我下一步需要做的是:
- 取值为
electricityUse
和waterUse
和他们timePasses()
其他staement - 内
timePasses()
else语句中存储,地方electrcityUse
值在ElectricMeter
类中incrementGenerated()
方法,为waterUse
变量这样做。
UPDATE
类已更新,仍然在努力找出如何使它工作。
感谢您的反馈,我对“私人ElectricMeter(){}”线路有点困惑。首先这是做什么的?其次,当我实现它时,我可以错误地说构造函数不能被应用,因为它没有参数,它需要int,int,int,int。 – Tom
这只是为了防止代码的其他部分创建仪表的实例。如果出现该错误,ElectricMeter似乎从超类继承。这只适用于米不从任何东西继承。 @Tom – Sweeper
仪表类继承了一个名为Meter的超类。我已经在我的问题中更新了我的课程,但仍然无法完全弄清楚如何使其工作。谢谢回复。 – Tom