用JAVA语言描述一个盒子类BOX,其有长,宽,高三个属性,并且能够设置每一个盒子的长,宽,高值和计算盒子的体积。再编写一个主类,在其主方法main()中创建一个具体的盒子,求出该盒子的体积
代码如下:如有帮助,请采纳一下,谢谢!
/**
*JAVA语言描述一个盒子类BOX,其有长,宽,高三个属性,并且能够设置每一个盒子的长,宽,高值和计算盒子的体积
*/
package Box;
public class Box {
/*
属性
*/
//公有属性:长
public int length;
//公有属性:宽
public int weight;
//公有属性:高
public int height;
/*
构造方法
*/
//无参构造方法
public Box(){
}
//有参构造方法
public Box(int length, int weight, int height){
this.length = length;
this.weight = weight;
this.height = height;
}
/*
方法
*/
//计算体积
public void volume(){
System.out.println("体积是:" + this.length * this.weight * this.height);
}
}
class Box{
double height;
double width;
doule length
getter......
setter.......
}
Box(double height.double width,double length){
this.height = height;this.width= width;this.length= length;
}
class Test{
public static void main(String[] args){
Box box = new Box(5.0,5.0,5.0);
double bukl = getBukl(box);
System.out.print("体积为:" + bukl)
}
static getBukl(Box box){
return box.getWidth() * box.getHeight() * box.getLength();
}
}