如何将值传递给此构造函数?

时间:2016-04-27 06:51:45

标签: java

我正在完成这项任务但却陷入了这一点。

如何将(length)和(width)的值传递给此构造函数?

UML(RoomDimension bin)。如果bin是一个对象。 http://i.imgur.com/OfF4nti.png

我的代码

 public class RoomDimension
{

private double length;
private double width;

public RoomDimension(double len, double w)
{
    length = len;
    width = w;
}

public double getArea()
{
    return (length*width);
}

public String toString()
{
    return String.format("%10s %10s %10s %10s", "Length: ", length, "Width: ", width);
}
}

我被困在下面的部分

public class RoomCarpet
     {
    private RoomDimension size;
    private double carpetCost;

    public void RoomCarpet(RoomDimension dim, double cost)
    {
        // how do use the parameter (object dim) to set size?
    }

    public double getTotalCost()
    {
        return (size.getArea())*carpetCost;
    }
     }

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您应该assign调暗到size

 public void RoomCarpet(RoomDimension dim, double cost)
    {
        // how do use the parameter (object dim) to set size?
        this.size = dim;
       // similarly for cost
        this.carpetCost = cost;
    }