【问题标题】:How to construct a method that needs to pass in the values from a constructor?如何构造一个需要从构造函数传入值的方法?
【发布时间】:2016-11-30 07:48:44
【问题描述】:

我正在编写一个程序,该程序基于记录房屋内电器使用的能源消耗量。到目前为止,我已经创建了各种仪表类,例如 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() else 语句中
  2. timePasses() else 语句中,将electrcityUse 的值放在incrementGenerated() 方法中的ElectricMeter 类中,并对waterUse 变量执行相同操作。

更新

课程已更新,仍在努力寻找如何使其发挥作用。

【问题讨论】:

    标签: java multiple-inheritance


    【解决方案1】:

    首先,我假设您有一个 Appliance 类,所有设备都从该类扩展。您应该在 Appliance 类中创建变量来存储电、气和水的使用情况:

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

    请注意,您应该始终使用 getter 和 setter 而不是公共字段。我只是懒惰:D

    更改您的构造函数,以便设置上面的变量:

    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;
    
    } 
    

    编写else 子句的一种方法是使用“单例模式”。

    在每个仪表类中,这样写:

    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);
    

    【讨论】:

    • 感谢您的反馈,我对private ElectricMeter() {} 行有点困惑。首先这是做什么的?其次,当我实现它时,我可能会报错说构造函数无法应用,因为它没有参数并且它需要 int、int、int、int。
    • 这只是为了防止代码的其他部分创建仪表的实例。如果您收到该错误,则 ElectricMeter 似乎继承自超类。这仅在仪表不继承任何东西时才有效。 @汤姆
    • 仪表类确实继承自一个名为 Meter 的超类。我已经在我的问题中更新了我的课程,但仍然无法弄清楚如何让它发挥作用。感谢您的回复。
    • 您应该将private ElectricMeter() {} private static ElectricMeter instance = new ElectricMeter(); public static ElectricMeter getInstance() { return instance; } 放在每个仪表类中。 @汤姆
    【解决方案2】:

    您应该检查您的设计。

    如果你需要访问一个类参数,你可以定义它public或者更好地创建一个返回值的所谓getter方法。

    例子:

    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;
    

    如果你将某个东西乘以 0,它总是 0...

    【讨论】:

      猜你喜欢
      • 2016-10-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 2011-03-24
      • 2020-04-11
      • 2010-09-22
      相关资源
      最近更新 更多