0

我正在编写一个程序,该程序调用同一文件夹中不同类的“发票”方法。我不断收到以下错误:

error: cannot find symbol

strInvoice = invoice();
             ^
symbol: method invoice()

这是我在程序中调用该方法的方式:

strInvoice= invoice();
JOptionPane.showInputDialog(null, strInvoice, "*Name*'s Party Store", -1);

这是该方法在位于同一文件夹中的类文件中的样子:

public String invoice()
{
    String strInfo=""; //string returned containing all the information
    double dBalloonNoDiscount;  //balloon total without discount
    double dCandleNoDiscount;   //candle total without discount
    double dBannerNoDiscount;   //banner total without discount
    double dSubtotal;       //subtotal
    double dTax;            //tax
    double dShippingCost;       //shipping cost
    double dTotal;          //total
    String strShippingType;     //shipping type

    dBalloonNoDiscount = balloons * 2.50;
    dCandleNoDiscount = candles * 6.00;
    dBannerNoDiscount = banners * 2.00;

    dSubtotal = subtotal();
    dTax = tax();
    dShippingCost = shippingCost();
    strShippingType = shippingType();
    dTotal = orderTotal();

    strInfo += "\nQuantity"
        + "\n\nBalloons: " + balloons + " @ $2.50 = " 
            + df.format(dBalloonNoDiscount) + "* Discount Rate: "
            + df.format(discount('B')) + " = " 
            + df.format(subtotal('B'))
        + "\n\nCandles: " + candles + " @ 6.00 = " 
            + df.format(dCandleNoDiscount) + "* Discount Rate: "
            + df.format(discount('C')) + " = " 
            + df.format(subtotal('C'))
        + "\n\nBanners: " + banners + " @ $2.50 = " 
            + df.format(dBannerNoDiscount) + "* Discount Rate: "
            + df.format(discount('N')) + " = " 
            + df.format(subtotal('N'))
        + "\nSubtotal: " + df.format(dSubtotal)
        + "\n  Tax: " + df.format(dTax)
        + "\nShipping: " + df.format(dShippingCost) + " - " 
            + strShippingType
        + "\n Total: " + dTotal;

    return strInfo;
}

我希望这是足够的信息。我似乎无法找到问题所在。

4

3 回答 3

2
I am writing a program that calls upon the "invoice" method from a different 
class in the same folder.

然后你这样称呼它:

strInvoice= invoice();

这行不通,因为您需要将此方法称为:

strInvoice = obj.invoice();

其中 obj 是另一个类的实例。

或者,如果invoice()方法是public static那么你也可以这样称呼它:

strInvoice = SomeClass.invoice();
于 2013-04-17T18:11:19.250 回答
1

如果您调用的方法在调用它的类之外,那么在调用方法之前您需要一个引用。

在您的情况下 invoice() 方法在其他类中可用,并且您在其他类中调用此方法,因此您需要在其中 invoice() 方法可用的类(对象)的引用。例如 :

  ClassA object = new ClassA();
  object.invoice();   // assume invoice method is available in ClassA.
于 2013-04-17T19:05:44.063 回答
0

如果您希望能够从不同的类调用方法,则需要将该方法设为静态,或者您需要获取定义它的类的实例。

在您的情况下,您可能需要一个实例,例如:

YourClass instance = new YourClass();

然后:

strInvoice = instance.invoice();
于 2013-04-17T18:11:11.447 回答