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