使用set方法允许另一个类访问数组

时间:2014-02-26 09:26:21

标签: java arrays

我在允许发票类访问processinvoice类中的数组时遇到问题。

这是我特别需要做的。

  • 使用set方法将InvoiceItem对象数组设置为Invoice。

我相信我需要通过setInvoiceItems传递数组,但我尝试过的一切都没有用。我已经尝试了最近几天仍然没有发现任何东西。 我相信这很容易,我只是在思考它,但我怎么做到这一点? 我的两个类的代码都在下面,

public class Invoice {


    private InvoiceItem[] invoiceItems;
    private double totalInvoice;

    public double getTotalInvoice() {
        return totalInvoice;
    }

     void setInvoiceItems(InvoiceItem[] inInvoiceItems) {
        invoiceItems = inInvoiceItems;

    }

    void calculateInvoice() {

        int counter = 0;
        while (counter < invoiceItems.length) {
            totalInvoice += invoiceItems[counter].calculateItemTotal();
            counter++;
        }

        System.out.println("Invoice total" + totalInvoice);

    }

    void displayInvoice() {

        int counter = 0;
        while (counter < invoiceItems.length) {
            invoiceItems[counter].display();
            counter++;
        }

    }
}

public class ProcessInvoice {

    Invoice[] invoices;

    void createInvoiceItems() {

        InvoiceItem[] items = new InvoiceItem[3];

        items[0] = new InvoiceItem();
        items[0].setItemId(123);
        items[0].setItemQuantity(55);
        items[0].setItemPrice(2.99);
        items[0].setItemDescription("Ball");


        items[1] = new InvoiceItem();
        items[1].setItemId(55);
        items[1].setItemQuantity(95);
        items[1].setItemPrice(0.5);
        items[1].setItemDescription("Nails");


        items[2] = new InvoiceItem();
        items[2].setItemId(4235);
        items[2].setItemQuantity(10);
        items[2].setItemPrice(5.68);
        items[2].setItemDescription("Soda");

        invoices.setInvoiceItems(items);
    }


    void runProcess() {

        Invoice[] invoices = new Invoice[1];
        createInvoiceItems();
        invoices[0].calculateInvoice();
        invoices[0].displayInvoice();



    }


}

6 个答案:

答案 0 :(得分:1)

Invoice[] invoices;是一个发票数组,方法setInvoiceItems(..)出现在Invoice个对象中。

您需要创建一个Invoice对象。 Invoice invoice = new Invoice();

然后设置InvoiceItems invoice.setInvoiceItems(items);

答案 1 :(得分:1)

您必须从InvoiceItem[]返回createInvoiceItems(),然后设置InvoiceItem[] into invoices[0] using the same method。一个片段如下所示。但是,这将设置相同的InvoiceItem[] in each Invoice of your Invoice[]

InvoiceItem[] createInvoiceItems() {

    InvoiceItem[] items = new InvoiceItem[3];

    items[0] = new InvoiceItem();
    items[0].setItemId(123);
    items[0].setItemQuantity(55);
    items[0].setItemPrice(2.99);
    items[0].setItemDescription("Ball");


    items[1] = new InvoiceItem();
    items[1].setItemId(55);
    items[1].setItemQuantity(95);
    items[1].setItemPrice(0.5);
    items[1].setItemDescription("Nails");


    items[2] = new InvoiceItem();
    items[2].setItemId(4235);
    items[2].setItemQuantity(10);
    items[2].setItemPrice(5.68);
    items[2].setItemDescription("Soda");

    return items;
}

void runProcess() {

    Invoice[] invoices = new Invoice[1];
    //create an Invoice Object
    Invoice invoice = new Invoice();
    invoices[0] = invoice;

    //createInvoiceItems(); // Not required here
    invoices[0].calculateInvoice();
    invoices[0].displayInvoice();
    invoices[0].setInvoiceItems(//call here createInvoiceItems());
}

答案 2 :(得分:0)

发票是一个数组,您正在初始化数组,但没有用实际的Invoice对象的任何实例填充它:

invoices[0] = new Invoice();

此外,方法createInvoiceItems()不会返回它创建的InvoiceItem数组,只有在方法完成执行后它才会丢失。

答案 3 :(得分:0)

您有以下选项

1)使ProcessInvoice类中的InvoiceItem数组为static,然后使用ProcessInvoice .InvoiceItem从Invoice类访问该数组。只有当你的设计被淘汰时才这样做。

在ProcessInvoice类中初始化InvoiceItem数组之后调用setInvoiceItems方法(小心你要调用的实例)

public class Invoice {
private InvoiceItem[] invoiceItems;
private double totalInvoice;

public double getTotalInvoice() {
    return totalInvoice;
}

 void setInvoiceItems() {
    invoiceItems = ProcessInvoice.Items;

}

void calculateInvoice() {

    int counter = 0;
    while (counter < invoiceItems.length) {
        totalInvoice += invoiceItems[counter].calculateItemTotal();
        counter++;
    }

    System.out.println("Invoice total" + totalInvoice);

}

void displayInvoice() {

    int counter = 0;
    while (counter < invoiceItems.length) {
        invoiceItems[counter].display();
        counter++;
    }

}
}

public class ProcessInvoice {

Invoice[] invoices;
static InvoiceItem[] items;

void createInvoiceItems() {

    InvoiceItem[] items = new InvoiceItem[3];

    items[0] = new InvoiceItem();
    items[0].setItemId(123);
    items[0].setItemQuantity(55);
    items[0].setItemPrice(2.99);
    items[0].setItemDescription("Ball");


    items[1] = new InvoiceItem();
    items[1].setItemId(55);
    items[1].setItemQuantity(95);
    items[1].setItemPrice(0.5);
    items[1].setItemDescription("Nails");


    items[2] = new InvoiceItem();
    items[2].setItemId(4235);
    items[2].setItemQuantity(10);
    items[2].setItemPrice(5.68);
    items[2].setItemDescription("Soda");
    new Invoice().setInvoiceItems();
}
void getInvoiceItems()
{
return items;
}

void runProcess() {

    Invoice[] invoices = new Invoice[1];
    createInvoiceItems();
    invoices[0].calculateInvoice();
    invoices[0].displayInvoice();



}


 }

答案 4 :(得分:0)

实际上,使用您的代码探测是通过Invoice的新对象调用setInvoiceItem方法,但在显示时您正在使用不同的对象。确保复制数据的数组和复制后要查找数据的数组具有相同的实例

答案 5 :(得分:-2)

您需要在ProcessInvoice类中为该数组实现getter。

public class ProcessInvoice {

    private Invoice[] invoices;

    ........

    public Invoice[] getInvoices() {
        return invoices;

    }
}

在你的Invoice班级中;

public class Invoice {


    private InvoiceItem[] invoiceItems;
    private double totalInvoice;
    private ProcessInvoice processInvoice;

    ......

    void getInvoices() {
        processInvoice = new ProcessInvoice();
        processInvoice.runProcess();
        processInvoice.getInvoices();

    }


}