为"无名的粉"写一个类: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-17 13:22:58 浏览: 80
class WuMingFen {
private String theMa; // 面码
private int quantity; // 粉的份量(两)
private boolean likeSoup; // 是否带汤
// 构造方法,初始化属性
public WuMingFen(String theMa, int quantity, boolean likeSoup) {
this.theMa = theMa;
this.quantity = quantity;
this.likeSoup = likeSoup;
}
// 重载构造方法,部分属性有默认值
public WuMingFen(String theMa, int quantity) {
this(theMa, quantity, false); // 默认不带汤
}
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;
}
}
// 构造出来的粉对象
WuMingFen f1 = new WuMingFen("牛肉", 3, true);
WuMingFen f2 = new WuMingFen("牛肉", 2); // 默认不带汤的粉对象
阅读全文