2016-11-30 81 views
1

我正在编写一个基于注册家中电器正在使用的能量消耗量的程序。到目前为止,我已经创建了各种仪表类,例如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() 
    { 

    } 

} 

我下一步需要做的是:

  1. 取值为electricityUsewaterUse和他们timePasses()其他staement
  2. timePasses() else语句中存储,地方electrcityUse值在ElectricMeter类中incrementGenerated()方法,为waterUse变量这样做。

UPDATE

类已更新,仍然在努力找出如何使它工作。

回答

0

首先,我假设你有一个Appliance类,所有的设备延伸。你应该建立在Appliance类存储电力,燃气及水的使用变量:

public class Appliance 
{ 
    public int electricityUse, gasUse, waterUse, timeOn; 
    // ... 
} 

注意,你应该总是使用 getter和setter方法,而不是公共领域。我只是懒惰:d使变量上面都将置

更改您的构造函数:写else条款

ElectricShower(int electricityUse, int gasUse, int waterUse, int timeOn) 
{ 
    super(electricityUse, gasUse, waterUse, timeOn); 
    // I don't know why you multiply the constant by incrementTime here. Seems weird. I think you can remove them. 
    this.electricityUse = 12 * incrementTime; 
    this.gasUse = 0 * incrementTime; 
    this.waterUse = 4 * incrementTime; 
    this.timeOn = 15 * incrementTime; 

} 

一种方法是使用“单例模式”。

在每米类,写这样的事:

private ElectricMeter() {} 
private static ElectricMeter instance = new ElectricMeter(); 
public static ElectricMeter getInstance() { return instance; } 

incrementConsumed方法,你应该接受一个参数,指示多少递增:

public int incrementConsumed(int value) 
{ 
    // logic here... 
} 

else条款,你可以这样做:

ElectricMeter.getInstance().incrementConsumed(electricityUse); 
GasMeter.getInstance().incrementConsumed(gasUse); 
WaterMeter.getInstance().incrementConsumed(waterUse); 
+0

感谢您的反馈,我对“私人ElectricMeter(){}”线路有点困惑。首先这是做什么的?其次,当我实现它时,我可以错误地说构造函数不能被应用,因为它没有参数,它需要int,int,int,int。 – Tom

+0

这只是为了防止代码的其他部分创建仪表的实例。如果出现该错误,ElectricMeter似乎从超类继承。这只适用于米不从任何东西继承。 @Tom – Sweeper

+0

仪表类继承了一个名为Meter的超类。我已经在我的问题中更新了我的课程,但仍然无法完全弄清楚如何使其工作。谢谢回复。 – Tom

0

你应该检讨y我们的设计。

如果您需要访问类的参数,你可以只把它定义公共或更好的创建一个返回值所谓吸气方法。

例子:

public class MyData { 
    public int counter; 
} 

.... 
// Some other class 
MyData data = new MyData(); 

data.counter = 5; 

System.out.println(data.counter); 

或者

public class MyData { 
    private int counter; 
    public void setCounter(int counter) { 
     this.counter = counter; 
    } 
    public int getCounter() { 
     return this.counter; 
    } 
} 

.... 
// Some other class 
MyData data = new MyData(); 

data.setCounter(5); 

System.out.println(data.getCounter()); 

在你的代码中,我看到:

public int incrementConsumed() 
{ 
    //Store value of electricityUse. 
} 

但这种方法应该只返回一个整数,也没有参数来获取输入储藏。

它应该是:

public void incrementConsumed(int amount) { 
    this.amount += amount; 
} 

我很担心这条线:

gasUse = 0 * incrementTime; 

如果你乘东西这将是永远 ...

相关问题