2016-03-30 6 views
-2

boxweightクラスのBoxクラスで定義されているgetvolume()メソッドの使用方法はありますか?私はそれによって定義されたメソッドを使用するためにBoxクラスをインスタンス化する必要があることを知っていますが、どうですか?クラスはJavaの別のクラスで定義されたメソッドをどのように使用しますか?

class Box { 
    private int lenght; 
    private int breadth; 
    private int height; 
    int price; 

    Box(int l, int b, int h) { 
     lenght= l; 
     breadth= b; 
     height= h; 
    } 

    public Box(int p) { 
     price= p; 
    } 

    double getvolume() { 
     return lenght*height*breadth; 
    } 

    void setsize(int $l, int $b, int $h ) { 
     lenght= $l; 
     breadth= $b; 
     height= $h; 
    } 
} 

public class boxclassdemo { 
    public static void main(String[] args) { 
     Box mybox1=new Box(10,10,10); 
     Box mybox2=new Box(5,5,5); 
     Box mybox3=new Box(20); 

     System.out.println(mybox1.getvolume()); 
     System.out.println(mybox2.getvolume()); 
     System.out.println(mybox3.price); 
    } 
} 

boxweightクラス:

public class boxweight { 
    int weight; 
    int length,breadth,height; 
    public static void main(String[] args) { 
     boxweight myboxx = new boxweight(); 
     myboxx.weight= 25; 
     myboxx.length=10; 
     myboxx.breadth=20; 
     myboxx.height=30; 
    } 
} 
+0

はBoxクラスのインスタンスを作成し、クラスのボックスのオブジェクトを定義し、そのインスタンス –

+0

それを使用すると、あなたはJavaのコーディング規則について学びたいと思っています。クラス、メソッドなどに名前を付ける方法に関する規則があります。あなたはそれらを知らないようです。あなたの実際の質問のために:それは超基礎知識のようなものです。ソートから...まあ、あなたは他の人に尋ねるべきではありません。あなたは書籍やチュートリアルに目を向ける必要があります。他の人に頼ってあなたに説明するのではなく、 –

+1

にメソッドを呼び出す必要があります – GhostCat

答えて

0

(ちなみに、クラス名の最初の文字は、通常、アップキャストすること
あなたはEclipseや他のIDEを使用している場合、それはときに、あなたが作成するのに役立ちます。フォローのステップを実行します。その後

Box box = new Box(25, 10, 20, 30);
を、
System.out.println(box.getvolume());

あなたは間違いなく必要がありますあなたの質問に答え、同様に他の多くの質問を見つけることができます
+0

あなたの答えをよりよくフォーマットするために時間をかけてください。ヒント:縦書きの間隔(段落と呼ばれる)は、通常、テキストを読みやすくします。それから:あなたは彼のコードから、彼は輸入が必要だと言うことはできません... – GhostCat

1

同じことが、実際にこれらを使用するために行くとすぐにOracle Java Tutorial on Object Creation

As you know, a class provides the blueprint for objects; you create an object from a class. Each of the following statements taken from the CreateObjectDemo program creates an object and assigns it to a variable: 

Point originOne = new Point(23, 94); 
Rectangle rectOne = new Rectangle(originOne, 100, 200); 
Rectangle rectTwo = new Rectangle(50, 100); 

The first line creates an object of the Point class, and the second and third lines each create an object of the Rectangle class. 

the very next tutorial

Code that is outside the object's class must use an object reference or expression, followed by the dot (.) operator, followed by a simple field name, as in: 

objectReference.fieldName 

私はあなたにこれらのチュートリアルを読んだり、あなたに良いJavaブックを手に入れることを勧めます。そこにはたくさんのものがあります。

0

継承を使用するように見えます。あなたはこのようBoxWeightを定義する場合、クラス名の後に「extends Boxを」注意:

public class BoxWeight extends Box { 
    int weight; 

    BoxWeight(int l, int b, int h, int w) { 
     super(l, b, h); 
     weight = w; 
    } 
} 

それはあなたがそうのように、それは公共の方法だ使用できることを意味ボックスオブジェクトであったかのように、あなたはboxweightオブジェクトを扱うことができます:

public static void main(String[] args) { 
    BoxWeight myboxx = new BoxWeight(10, 20, 30, 25); 
    double volume = myboxx.getvolume(); 
} 
関連する問題