为“无名的粉”写一个类:class WuMingFen 要求:
(1.有三个属性:面码:String theMa
粉的分量(两):int quantity
是否带汤:boolean likeSoup
(2.写一个构造方法,以便于简化初始化过程,如:
WuMingFen f1 = new WuMingFen(“牛肉”,3,true);
(3.重载构造方法,使得初始化过程可以多样化:
WuMingFen f2 = new WuMingFen(“牛肉”,2);
(4.如何使得下列语句构造出来的粉对象是酸辣面、2两、带汤的?
WuMingFen f3 = new WuMingFen();
(5.写一个普通方法:check(),用于查看粉是否带汤,并且把内容打印出来。
即:将对象的三个属性打印在控制台上。打印结果如下:
牛肉面,3两,带汤
牛肉面,2两,不带汤
酸辣面,4两,带汤
package lianxi03;
public class WuMingFen {
public String name;//面码
public double fl;//分量
public boolean isTang;//是否带汤
public WuMingFen() {
name = "酸辣";
fl = 2;
isTang = true;
check();
}
public WuMingFen(String name, double fl) {
this.name = name;
this.fl = fl;
check();
}
public WuMingFen(String name, double fl, boolean isTang) {
this.name = name;
this.fl = fl;
this.isTang = isTang;
check();
}
public void check(){
if(isTang){
System.out.println(name+"面,"+fl+"两,带汤");
}else{
System.out.println(name+"面,"+fl+"两,不带汤");
}
}
}
================================================================
package lianxi03;
public class Test {
public static void main(String[] args) {
WuMingFen f3 = new WuMingFen();
WuMingFen f2 = new WuMingFen("牛肉",2);
WuMingFen f1 = new WuMingFen("牛肉",3,true);
}
}