我为什么不断获取构造函数未定义的Java

问题描述 投票:-1回答:2

我需要有人向我解释这个好处,并且清楚地我基本上写了一个小程序,该程序具有三个类。地毯,计算器和地板。

因此将通过使用主类中的类计算器来调用地毯类与地板类相乘。但是我发现在计算器类中使用double作为返回类型时遇到了未定义的构造函数?我发现这是计算器类的问题,我有一个方法getTotalCost返回地板和地毯的成本。为什么不能在main中调用时仅将两个对象作为参数传递给计算器。我在计算器类中使用了double的原始返回类型。

有人可以帮我重写计算器并向我解释为什么我不能使用原始类型类,但是我必须在地板和地毯的计算器字段声明中使用类名称,为什么必须使用地板和地毯的类名称地毯将在构造函数中传递以构建对象,以及getTotalCost方法中的修复方法是什么,就像您使用类名声明两个实例字段一样,那么错误肯定会是这样的:“-操作符*未定义论点类型地板,地毯-发生“地毯”-1条更改的行“

尝试使用创建的类但出错。

Carpet carpet = new Carpet(3.5);
Floor floor = new Floor(2.75, 4.0);

通过计算器参数中的地板和地毯。//未定义构造函数的计算器Calculator = new Calculator(floor,carpet);

public class Calculator {

    private double floor;
    private double carpet;

    public Calculator() {
        // TODO Auto-generated constructor stub

    }
    public Calculator(double floor, double carpet) {
        // TODO Auto-generated constructor stub
        this.carpet=carpet;
        this.floor=floor;
    }

    public double getTotalCost() 
    {
        return (this.floor*this.carpet);
    }
}


public class Floor {

private double width;
private double length;

    public Floor()
    {
    }
    public Floor(double width,double length)
    {
        this.length=length;
        this.width=width;
    }
    public void setWidth(double width) 
    {
        if (this.width < 0) {
            this.width=0;
        }
        else {
            this.width=width;
        }
    }
    public void setLength(double length) 
    {
        if (this.length < 0) {
            this.length=0;
        }
        else {
            this.length=length;
        }
    }
    public double getArea() 
    {
        return (this.length * this.width);
    }
}


public class Carpet {

    private double cost;

    public Carpet()
    {
    }
    public Carpet(double cost)
    {
        this.cost=cost;
    }

    public void setCost(double cost) 
    {
        if (cost < 0) {
            this.cost=0;
        }
        else {
            this.cost=cost;
        }
    }
    public double getCost() 
    {
        return this.cost;
    }
}
java
2个回答
0
投票

由于您的Calculator类只有默认的构造函数(一个没有任何args),并且只接受double, double的构造函数,因此无法创建提供Calculator实例和CarpetFloor实例。实例。

所以您有2个选择。

  • 将建筑面积和地毯成本传递给当前的构造函数。

    Calculator calculator = new Calculator(floor.getArea(), carpet.getCost());

  • 将构造函数更改为接受Carpet实例和Floor实例(然后还应修改Calculator类逻辑)

public class Calculator {

    private Floor floor;
    private Carpet carpet;

    public Calculator() {
        // TODO Auto-generated constructor stub

    }
    public Calculator(Floor floor, Carpet carpet) {
        // TODO Auto-generated constructor stub
        this.carpet=carpet;
        this.floor=floor;
    }

    public double getTotalCost() 
    {
        return (this.floor.getArea() * this.carpet.getCost());
    }
}



0
投票

Calculator类的参数为(double, double),因此它将不接受Floor和Carpet对象。您可以像这样调用构造函数:

Calculator calc = new Calculator(carpet.getCost(), floor.getCost());

或将构造函数更改为以下内容:

Calculator (Floor f, Carpet c) {
    this.floor = f.getCost();
    this.carpet = c.getCost();
}
© www.soinside.com 2019 - 2024. All rights reserved.