调用方法时如何传递正确的值?

时间:2016-12-01 07:10:09

标签: java methods

我正在执行一项任务,其中我创建了一个其中包含timePasses()方法的Appliance类。此方法重定向某些值,这些值需要存储在另一个类中的另一个方法中。以下是我对此的看法:

电器

public class ElectricCooker extends Cooker {

    public int isOn = -1;
    public int isOff = 0;
    public int incrementTime;
    public int varPass = -1;

    @Override
    public int currentState() {
        if (varPass == 0) {
            return isOff;
        } else {
            return isOn;
        }
    }

    @Override
    public void useTime(int defaultTime) {
        defaultTime = 15;
        incrementTime = 4;
    }

    @Override
    public void timePasses() {
        if (varPass == isOff) {
            varPass = 0;
        } else {
            ElectricMeter.getInstance().incrementConsumed(electricityUse);
            GasMeter.getInstance().incrementConsumed(gasUse);
            WaterMeter.getInstance().incrementConsumed(waterUse);
        }
    }

    ElectricCooker(int electricityUse, int gasUse, int waterUse, int timeOn) {
        super(electricityUse, gasUse, waterUse, timeOn);

        this.electricityUse = 5 * incrementTime;
        this.gasUse = 0 * incrementTime;
        this.waterUse = 0 * incrementTime;
        this.timeOn = 15 * incrementTime;
    }
}

仪表

public class ElectricMeter {

    ElectricMeter() {
    }
    private static ElectricMeter instance = new ElectricMeter();

    public static ElectricMeter getInstance() {
        return instance;
    }

    public void incrementConsumed(int value) {
        System.out.println(value);
    }

    public int incrementGenerated() {
    }

    public boolean canGenerate() {
    }

    public String getConsumed() {
    }

    public String getGenerated() {
    }
}

主要方法

public class MainCoursework {

    public static void main(String[] args) {
        ElectricMeter a = new ElectricMeter();
        a.incrementConsumed(//what goes here?);     
    }
}

因此timePasses()的值已重定向到ElectricMeter个实例,但现在我需要将该值返回到米级中的increentConsumed()方法并且我被卡住了关于如何做到这一点。由于electricityConsumed的值为20,因此输出应为20。但我必须将一个参数传递给a.incrementConsumed(//pass parameter here),传递的内容将被打印到屏幕上而不是20中的electrictyUse。任何有关如何做到这一点的帮助表示赞赏,谢谢。

3 个答案:

答案 0 :(得分:3)

实际上,incrementConsumed方法确实如您所描述的那样实现:

public void incrementConsumed(int value)  
{
    System.out.println(value);     
}

一个名为incrementXXX的方法不应该输出任何东西,不是吗?它应递增变量/字段:

private int electricityUsed = 0;
public void incrementConsumed(int value)  
{
    electricityUsed += value;    
}

您应该声明另一个返回electricityUsed的方法:

public int getElectricityUsed() {
    return electricityUsed;
}

现在让我们修复您的main方法。

在您的主要方法中,您甚至没有创造任何消耗电力的东西!电表怎么样incrementConsumed?因此,从主方法中删除所有内容并创建一个炊具:

// your constructor looks weird. So I passed in some random arguments..
ElectricCooker cooker = new ElectricCooker(20, 0, 0, 60);

现在调用timePasses来模拟一段时间过去了:

cooker.timePasses();

并打印使用的电力:

System.out.println(ElectricMeter.getInstance().getElectricityUsed());

答案 1 :(得分:2)

您需要在ElectricMeter中创建一个实例变量,并在incrementConsumed上更新该值。如果要打印使用此变量的accessor

public class Electric {
    public static void main(String[] args) {
        ElectricCooker cooker = new ElectricCooker(1,2,3,4);
        //opertion on cooker
        //ignoring best way for singleton creation
        int electricityUse = ElectricMeter.getInstance().getElectricityUse();
        System.out.println(electricityUse);
    }
}

class ElectricCooker // extends Cooker
{
    public int isOn = -1;
    public int isOff = 0;
    public int incrementTime;
    public int varPass = -1;
    public int electricityUse = -1;

    public int currentState() {
        if (varPass == 0)
            return isOff;
        else {
            return isOn;
        }
    }

    public void useTime(int defaultTime) {
        defaultTime = 15;
        incrementTime = 4;
    }

    public void timePasses() {
        if (varPass == isOff)
            varPass = 0;
        else {
            ElectricMeter.getInstance().incrementConsumed(electricityUse);
        }
    }

    ElectricCooker(int electricityUse, int gasUse, int waterUse, int timeOn) {
        this.electricityUse = 5 * incrementTime;
    }

}

class ElectricMeter {

    public int electricityUse = -1;

    private static ElectricMeter instance = new ElectricMeter();

    public static ElectricMeter getInstance() {
        return instance;
    }

    public void incrementConsumed(int value) {
        this.electricityUse = value;
    }

    public int getElectricityUse() {
        return electricityUse;
    }
}

答案 2 :(得分:1)

ElectricMeter中,某些操作无法执行应有的操作。

  ElectricMeter.getInstance().incrementConsumed(electricityUse);  

应该增加一些内容,但它只在输出中写入:

public void incrementConsumed(int value){
         System.out.println(value);     
    }

你应该写它:

public void incrementConsumed(int value){
           consumed+=value; 
    }

并在private int consumed类中添加ElectricMeter字段以存储实际消耗的内容。

您的getConsumed()执行空白:

public String getConsumed(){

}

应该只返回consumed字段,您应该返回int值,而不是String

public int getConsumed()  {
  return consumed;
}

通过这种方式,您可以:

public static void main(String[] args){
  ElectricMeter.getInstance().incrementConsumed(20);     
  int consumed = ElectricMeter.getInstance().getConsumed();     
}