开灯(面向对象复习)
设计一个台灯类(Lamp)其中台灯有灯泡类(Buble)这个属性,还有开灯(on)这个方法
设计一个灯泡类(Buble),灯泡类有发亮的方法
其中有红灯泡类(RedBuble)和绿灯泡类(GreenBuble)他们都继承灯泡类(Buble)一个发亮的方法
请设计出一段代码可以使台灯开启灯泡发亮,并且保证替换不同种类的灯泡,台灯类代码不被修改
public class Lamp { private Buble buble;//灯泡 public Buble getBuble() { return buble; } public void setBuble(Buble buble) { this.buble = buble; } /** * 通过父类Buble调用子类重写父类的方法 * @param buble */ public void on(Buble buble){ if(buble instanceof RedBuble){ String r=buble.bright(); System.out.println("打开台灯"+r); }else if(buble instanceof GreenBuble){ String g=buble.bright(); System.out.println("打开台灯"+g); } } }
public abstract class Buble { public Buble(){ } public abstract String bright(); }
public class RedBuble extends Buble { //重写父类抽象方法 public String bright(){ return "红灯泡发亮"; } }
public class GreenBuble extends Buble{ //重写父类抽象方法 public String bright(){ return "绿灯泡发亮"; } }
public class BubleTest { public static void main(String[] args){ Lamp lamp=new Lamp(); //父类引用指向子类对象 Buble red=new RedBuble(); Buble green=new GreenBuble(); lamp.on(green); } }
运行结果:
打开台灯绿灯泡发亮