JAVA的作业,求大神啊~构造一个立方体类,类的构成包括如下内容:(1)成员变量为长、宽、高;(2)初始化立方体(设置长

JAVA的作业,求大神啊~
构造一个立方体类,类的构成包括如下内容:
(1)成员变量为长、宽、高;
(2)初始化立方体(设置长、宽、高)利用构造方法完成:要求重载构造方法,一个构造方法是初始化立方体长宽高为默认的10,10 和10,另外一个构造方法是传递用户指定的长宽高。
(3)修改立方体长宽高的方法;
(4)计算体积的方法。
befei 1年前 已收到1个回答 举报

猫猫袄 幼苗

共回答了20个问题采纳率:100% 举报

public class Cube {
private int length;
private int width ;
private int height;
public Cube(int length, int width, int height) {
super();
this.length = length;
this.width = width;
this.height = height;
}
public Cube() {
this.length = 10;
this.width = 10;
this.height = 10;
}
public void setLength(int length) {
this.length = length;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getVolume(){
return length*width*height;
}
public static void main(String[] args) {
Cube cube= new Cube();
System.out.println(cube.getVolume());
Cube cube2= new Cube(20,30, 40);
System.out.println(cube2.getVolume());
}
}

1年前

3
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.035 s. - webmaster@yulucn.com