【问题标题】:Trying to one class constructor to use some of the parameters from another class constructor试图让一个类构造函数使用来自另一个类构造函数的一些参数
【发布时间】:2016-04-18 16:15:18
【问题描述】:

抱歉,这可能是一个简单的解决方案,但我在编写代码来创建一个对象时遇到了麻烦,该对象将使用 Java 中另一个类构造函数参数中的参数。本质上,我有这个具有某些参数的 Inv 对象,并且我正在尝试仅使用 Inv 对象中的某些参数创建一个 Warehouse 对象,然后将在 Warehouse Transaction 类中使用该对象。我可以在 Inv 对象构造函数中执行 if 语句吗?不管怎样,我稍后会将这个对象推送到堆栈上,但我无法开发创建第二个对象的逻辑。请帮忙,我对此很陌生。

public class InvTrans

{

private int InvTransIndex = 0;
private Inv[] transactionArray;

public InvTrans()
{
    this.transactionArray = new Inv[100];
}

public static void main(String args[])
{
    InvTrans widget = new InvTrans();



    Inv order1 = new Inv("March 5th, 2016", "Received", 20, 0001, 2.10, 2.20);
    Inv order2 = new Inv("March 6th, 2016", "Received", 100, 0002, 2.10, 2.50);
    Inv order3 = new Inv("March 7th, 2016", "Received", 100, 0003, 2.10, 2.50);
    Inv order4 = new Inv("March 12th, 2016", "Sold", 140, 0004, 2.40, 2.60);


    widget.addLine(order1);
    widget.addLine(order2);
    widget.addLine(order3);
    widget.addLine(order4);



    for (int counter = 0; counter < widget.transactionArray.length; counter++)
    {
        widget.transactionArray[counter].display();
    }

}

public void addLine(Inv a)
{

    transactionArray[InvTransIndex] = a;
    InvTransIndex = InvTransIndex + 1;
}
}

class Inv
{
String date, type;
int units, referenceNumber;
double costPerUnit, pricePerUnit;

Inv(String d, String t, int u, int r, double c, double p) {
    this.date = d;
    this.type = t;
    this.units = u;
    this.referenceNumber = r;
    this.costPerUnit = c;
    this.pricePerUnit = p;


    Warehouse warehouseTransaction = new Warehouse(this.date, this.type, this.units, this.costPerUnit);

}

public void display()
{
     System.out.println(this.date + "\t" + this.type + "\t" + this.units + "\t\t\t\t\t" + this.referenceNumber + "\t\t\t\t\t" + this.costPerUnit + "\t\t\t" + this.pricePerUnit);
}
}    

public class Warehouse
{
    String date, type = "";
    int units = 0;
    double costPerUnit = 0;
    Warehouse(String d, String t, int u, double c)
    {
        date = d;
        type = t;
        units = u;
        costPerUnit = c;
    }
}

【问题讨论】:

  • 我不知道你想完成什么。通常,您有一个仓库,并且仓库中存放着物品。
  • 如果你想在每次创建 Inv 时都创建 Warehouse 对象,你当前的实现有什么问题?我只是将仓库设为类变量,以便您以后可以访问它
  • @Toaster 我试图弄清楚每次创建 Inv 对象时如何创建仓库对象,Inv 中的参数来满足它。我不知道该怎么做。
  • ...但这就是您已经做的...或者您想从构造函数外部创建仓库?
  • 我想我正在尝试在构造函数之外构造它。抱歉,我很难解释我的意图。

标签: java object constructor constructor-overloading


【解决方案1】:

您可以使用工厂模式:

创建一个名为 InvFactory 的类。这个类将可以访问仓库(顺便说一下,仓库是你存储所有物品的地方......每次创建一个 Inv 时创建一个新对象仓库对我来说真的没有意义。在这里我而是将这些对象称为 WarehouseItem)。当你想创建一个 Inv 时,你将调用工厂而不是 Inv 构造函数

public class InvFactory
{ 
    private LinkedList<WarehouseItem> _warehouse = new LinkedList<WarehouseItem;

    public CreateInv(String d, String t, int u, int r, double c, double p)
    {
        Inv inv = new Inv(d, t, u, r, c, p);
        _warehouse.add(new WarehouseItem(d, t, u, c);
        return inv;
    }

    public LinkedList<WarehouseItem> getWarehouse()
    {
        return _warehouse;
    }
}

在你的主要方法中,你可以替换

Inv order1 = new Inv("March 5th, 2016", "Received", 20, 0001, 2.10, 2.20);
Inv order2 = new Inv("March 6th, 2016", "Received", 100, 0002, 2.10, 2.50);
Inv order3 = new Inv("March 7th, 2016", "Received", 100, 0003, 2.10, 2.50);
Inv order4 = new Inv("March 12th, 2016", "Sold", 140, 0004, 2.40, 2.60);

通过

InvFactory factory = new InvFactory();

Inv order1 = factory.CreateInv("March 5th, 2016", "Received", 20, 0001, 2.10, 2.20);
Inv order2 = factory.CreateInv("March 6th, 2016", "Received", 100, 0002, 2.10, 2.50);
Inv order3 = factory.CreateInv("March 7th, 2016", "Received", 100, 0003, 2.10, 2.50);
Inv order4 = factory.CreateInv("March 12th, 2016", "Sold", 140, 0004, 2.40, 2.60);

此时,您可以通过调用查看列表中的所有项目。

factory.getWarehouse();

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 2014-03-12
    • 2012-06-22
    • 1970-01-01
    • 2014-08-01
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多