2013-04-17 83 views
0

我正在编写一个程序,它调用来自同一文件夹中不同类的“发票”方法。我不断收到以下错误:Java - 错误:找不到符号

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; 
} 

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

+0

我建议你回顾一些OO和Java的基础知识,尝试做几个简单的例子并尝试这个。从长远来看,这肯定会有所帮助。 – Krishna

回答

0

如果您希望能够从另一个类中调用某个方法,则需要将该方法设置为静态或您需要获取定义该类的实例。

在你的情况,你可能需要一个实例,是这样的:

YourClass instance = new YourClass(); 

再后来:

strInvoice = instance.invoice(); 
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(); 
+0

非常感谢您的帮助! – Logan94

1

如果要调用这是它被称为类外的方法,那么你调用之前需要参考方法。

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

ClassA object = new ClassA(); 
    object.invoice(); // assume invoice method is available in ClassA. 
+0

感谢您的帮助! – Logan94