2012-04-14 16 views
2
class box { 
    double ht,wdt,len; 

    box(double h,double w,double l) { 
    ht=h; 
    wdt=w; 
    len=l; 
    } 

    double volume() { 
    return ht*wdt*len; 
    } 
} 


class boxme { 
    public static void main(String args[]) { 
    box mybox= new box(1,2,3); 
    System.out.print("The volume is "+mybox.volume()); 
    } 
} 

//このコードをbluejで実行するには、オブジェクト作成後に引き続き引数を与える必要があります(私は既にコードで与えていますが)。同じコードがcmdでうまく機能しますが、 bluejで試行されたとき。理由と解決策を提供して、bluejとcmdの間の等価性を引き出しますか? //bluejでコンパイルしますか?

+0

クラス名の最初の文字を大文字にすることを強くお勧めします。残念ながら私はしばらくブルージャーを使用していない。私は大ファンではない! – Arth

答えて

0

すでにmain関数が定義されているため、BlueJで明示的に実行している間にオブジェクトを作成する必要はありません。

クラスを右クリックし、public static void main(String args []) 関数を実行します。

+0

なぜbluejはあらかじめ定義された引数を受け取りませんか? –

+0

どのように実行していますか?スクリーンショットを表示しますか?ここにアップロードしてくださいhttp://imagebin.org/index.php?page=add – sbose

1

2つの異なるクラスがあり、他のクラスのメソッドを使用する場合は、そのクラスのインスタンスを作成する必要があります。

2番目のクラスを右クリックし、public static void main(String args[])関数を実行します。

クラスの名前は大文字で始まり、セキュリティのためにフィールドの範囲がprivateでなければならないことに注意してください。オブジェクトは常に小文字にする必要があります。

public class Box { 
    private double ht,wdt,len; 

    public Box(double h,double w,double l) { 
    ht=h; 
    wdt=w; 
    len=l; 
    } 

    public double volume() { 
    return ht*wdt*len; 
    } 
} 
public class boxme { 
    public static void main(String args[]) { 
    Box mybox= new Box(1,2,3); 
    System.out.print("The volume is "+mybox.volume()); 
    } 
} 
関連する問題