java中长方体的类是什么_编写java程序:定义一个描述长方体的类Box,要求如下...
匿名用戶
1級
2011-10-12 回答
//這個方法,完全可以完成你的要求,希望你可以理解哦!!加油,祝你學習進步~~~
class Box {
double width;
double height;
double depth;
public Box() { // 無參構造方法
width = 1.0;
height = 1.0;
depth = 1.0;
}
public Box(double w, double h, double d) { // 有參構造方法
width = w;
height = h;
depth = d;
}
void getBox() { // 獲得盒子信息
System.out.println("The width of Box is :" + width);
System.out.println("the heigth of Box is :" + height);
System.out.println("The depth of Box is :" + depth);
}
void volume() { // 計算體積函數
System.out.println("The volume of Box is:" + (width * height * depth));
}
void area() { // 計算表面積函數
System.out.println("The area of Box is :" + 2
* (width * height + width * depth + height * depth));
}
}
public class Tbox{
public static void main(String[] args) {
Box box1 = new Box(); // 使用無參構造方法實例化一個對象
System.out.println("第一個盒子:");
box1.getBox();
box1.volume();
box1.area();
System.out.println("第二個盒子");
Box box2 = new Box(5, 2, 5); // 使用有參構造方法實例化一個對象
box2.getBox();
box2.volume();
box2.area();
}
}
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的java中长方体的类是什么_编写java程序:定义一个描述长方体的类Box,要求如下...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 登录下线_java web中L
- 下一篇: jquery链式写法 java_jQue