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 に答える