public class WuMingFen {
String theMa = "酸辣";
int quantity = 2;
boolean soup = false;
public WuMingFen() {
soup = true;
}
public WuMingFen (String theMa, int quantity, boolean soup){
this.theMa = theMa;
this.quantity = quantity;
this.soup= soup;
}
public WuMingFen (String theMa, int quantity){
this.theMa = theMa;
this.quantity = quantity;
}
public void check(){
System.out.println("面码:"+ theMa +",粉的份量:"+ quantity +"两,是否带汤:"+ soup);
}
}
为什么有下面这个构造函数?
public WuMingFen() {
soup = true;
}
为了你调用他们的时候,soup的默认值不一样而已。比如你在测试类中定义一个有参构造和一个无参构造:
WuMingFen wmf=new WuMingFen(); //无参构造的soup默认值为:soup=true;
WuMingFen wmf=new WuMingFen(“酸辣”, 2, false); //有参构造的soup从这里传值