java - 使用构造函数和获取另一个类的方法的问题

标签 java constructor

因此,当我尝试运行用户类时,我不断收到错误消息,说需要 double,但没有找到任何参数。我在第 17、40、42、44、46 和 48 行收到错误。它们都是表示需要 double 的错误。用通俗易懂的英语回答我们将不胜感激。

我的主课:

import java.util.Scanner;

public class ElectricityCalculatorUser {

    //Main method

    public static void main (String [] args) {

        ElectricityCalculator myCalculator = new ElectricityCalculator();  

        Scanner input = new Scanner (System.in);

        //Input the initial reading 
        double initialReading;
        System.out.print ("What is the inital reading on your electricity meter in kwH? ");
        initialReading = input.nextDouble ();

        //Input the final reading
        double finalReading;
        System.out.print ("What is the final reading on your electricity meter in kwH? ");
        finalReading = input.nextDouble ();

        //Input the number of days
        //between readings
        double numberOfDays;
        System.out.print ("How many days have passed between your initial and final reading? ");
        numberOfDays = input.nextDouble ();

        //Calculations
        double totalElectricityUsed = myCalculator.totalElectricityUsed();
        System.out.print ("Total electricity used = " + totalElectricityUsed);
        double costOfElectricity = myCalculator.costOfElectricity();
        System.out.print ("Cost of electricity = " + costOfElectricity);
        double standingCharge = myCalculator.standingCharge();
        System.out.print ("Standing charge = " + standingCharge);
        double costBeforeVAT = myCalculator.costBeforeVAT();
        System.out.print ("Cost before VAT is added = " + costBeforeVAT);
        double VAT = myCalculator.VAT();
        System.out.print ("Cost of VAT = " + VAT);
        double totalCost = myCalculator.totalCost();
        System.out.print ("Total cost = " + totalCost);
        }   
}   

包含所有方法的我的类:

public class ElectricityCalculator {

    //Attributes
    private double initialReading;
    private double finalReading;
    private double numberOfDays;

    //Constructors
    public ElectricityCalculator (double ir, double fr, double nod) {
        initialReading = ir;
        finalReading = fr;
        numberOfDays = nod;
        }

    //Calculating total electricity used
    public double totalElectricityUsed () {
        return finalReading - initialReading;
        }

    //Calculating cost of electricity
    public double costOfElectricity () {
        return totalElectricityUsed * 0.175;
        }

    //Calculating standing charge
    public double standingCharge (double numberOfDays) {
        return numberOfDays * 0.25;
        }

    //Calculating cost before VAT is added
    public double costBeforeVAT (double costOfElectricity, double standingCharge) {
        return costOfElectricity + standingCharge;
        }

    //Cost of VAT
    public double VAT (double costBeforeVAT) {
        return costBeforeVAT * 0.05;
        }

    //Total cost of electricity used 
    //including VAT  
    public double totalCost (double costBeforeVAT, double VAT) {
        return costBeforeVAT + VAT;
        }

}   

最佳答案

在 java 中,如果您不编写构造函数,系统会自动为您添加一个默认构造函数,并且此构造函数将是 public 且不带任何参数。

类似下面的内容:

public ElectricityCalculator () {

}

但是,当您定义任何构造函数时,默认构造函数将被删除。因此,您类(class)中唯一的构造函数是

public ElectricityCalculator (double ir, double fr, double nod) {
  initialReading = ir;
  finalReading = fr;
  numberOfDays = nod;
}

因此

ElectricityCalculator myCalculator = new ElectricityCalculator(); 

不匹配任何构造函数。

您可以在获取构造对象所需的值后简单地创建实例

ElectricityCalculator myCalculator = new ElectricityCalculator(initialReading, finalReading, numberOfDays); 

关于java - 使用构造函数和获取另一个类的方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26701913/

相关文章:

java - 使用另一个数组的大小初始化多维数组而不处理java中的元素

java - Java中将时间分成相等的间隔

c++ - 如何初始化需要执行计算的 const 成员?

C++无法实现默认构造函数

java - 关于构造函数的困惑

c# - 在具有多个构造函数的单个类中使用对象

java - 类型不匹配 : cannot convert from ViewGroup. LayoutParams 到 LinearLayout.LayoutParams

java - 使用 BufferedReader 读取大型文本文件,打印每一行会减慢该过程的严重程度吗?

java - ZonedDateTime 中的 plusSeconds 未按预期工作

Java:当我实例化抽象类的子类时,它无法识别其父类(super class)的构造函数