我创建了一个父类
public class Appliances {
private boolean hasWorkToDo;
public Appliances(boolean hasWorkToDo) {
this.hasWorkToDo = hasWorkToDo;
}
现在我有一个儿童班
class CoffeMaker extends Appliances {
public CoffeMaker(boolean hasWorkToDo) {
super(hasWorkToDo);
}
public void brewCoffee() {
if (hasWorkToDo = true) { // I have question how to bring
// hasWorkToDo value on this line
System.out.println("Coffee is brewing");
}
我有另一个类,我在其中创建了这个咖啡机酿酒大师的对象
public class SmartKitchen {
private CoffeMaker brewMaster;
public void addWater() {
brewMaster = new CoffeMaker(true);
}
}
请指导我如何在Coffemaker类brewcoffee方法中编写if条件,以便当我运行addWater()方法时,这个true到达if条件并打印“Coff正在酿造”语句。 非常感谢您的帮助。
我尝试了很多方法,但都不起作用。
要继承字段,您需要创建它们
protected
protected boolean hasWorkToDo;
然后,您需要比较变量,而不是分配它们
public void brewCoffee() {
if (hasWorkToDo) {
System.out.println("Coffee is brewing");
}
}
然后调用该函数,因为构造函数没有打印任何内容
public void addWater() {
brewMaster = new CoffeMaker(true);
brewMaster.brewCoffee();
}