为"无名的粉"写一个类:class WuMingFen 要求: 1.有三个属性:面码:String theMa 粉的份量(两):int quantity 是否带汤:boolean likeSoup 2.写一个构造方法,以便于简化初始化过程,如: WuMingFen f1 = new WuMingFen("牛肉",3,true); 3.重载构造方法,使得初始化过程可以多样化: WuMingFen f2 = new WuMingFen("牛肉",2); 4.如何使得下列语句构造出来的粉对
时间: 2024-04-18 22:23:22 浏览: 156
class WuMingFen {
private String theMa;
private int quantity;
private boolean likeSoup;
// 构造方法1
public WuMingFen(String theMa, int quantity, boolean likeSoup) {
this.theMa = theMa;
this.quantity = quantity;
this.likeSoup = likeSoup;
}
// 构造方法2,重载构造方法1
public WuMingFen(String theMa, int quantity) {
this.theMa = theMa;
this.quantity = quantity;
this.likeSoup = false; // 默认不带汤
}
// Getter 和 Setter 方法
public String getTheMa() {
return theMa;
}
public void setTheMa(String theMa) {
this.theMa = theMa;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public boolean isLikeSoup() {
return likeSoup;
}
public void setLikeSoup(boolean likeSoup) {
this.likeSoup = likeSoup;
}
}
```
使用以上的类,下面的语句可以构造出所需的粉对象:
```java
WuMingFen f1 = new WuMingFen("牛肉", 3, true);
WuMingFen f2 = new WuMingFen("牛肉", 2);
```
其中,f1 为牛肉面,份量为3两,带汤;f2 为牛肉面,份量为2两,不带汤。
阅读全文