Java编程题:定义一个Box类,属性有:长,宽,高,并初始化数据
Box类:
/**
* 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);
}
}
Box测试类:
/**
* Box的测试类
*/
package Box;
public class BoxText {
public static void main(String[] args) {
//构造盒子
Box box = new Box(3, 4, 5);
//输出信息
box.volume();
}
}
结果:
体积是:60
进程已结束,退出代码为 0