2016-11-30 74 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; 

如果你乘東西這將是永遠 ...

相關問題