用java,设计一个Box类,拥有材质,长度,宽度,高度,价格等属性;对各个属性的setter方法和getter方法。
时间: 2024-03-27 22:36:56 浏览: 107
好的,以下是Java实现的Box类:
```java
public class Box {
private String material;
private double length;
private double width;
private double height;
private double price;
public Box(String material, double length, double width, double height, double price) {
this.material = material;
this.length = length;
this.width = width;
this.height = height;
this.price = price;
}
public String getMaterial() {
return material;
}
public void setMaterial(String material) {
this.material = material;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Box{" +
"material='" + material + '\'' +
", length=" + length +
", width=" + width +
", height=" + height +
", price=" + price +
'}';
}
}
```
在这个Box类中,我定义了五个属性:材料(material)、长度(length)、宽度(width)、高度(height)和价格(price),并且每个属性都有对应的getter和setter方法。我还重写了toString方法,以便在需要打印对象时更方便。
阅读全文