笔记本充电
- 一 、LapTop类
- 二、AcPower类
- 三、Battery类
- 四、Power类
- 五、TestLapTop类
- 六、效果展示
题目要求
一 、LapTop类
package lapTop;public class LapTop {public void charge() {System.out.println("供电中");}}
二、AcPower类
package lapTop;public class AcPower extends LapTop {@Overridepublic void charge() {System.out.println("AcPower供电中。");}
}
三、Battery类
package lapTop;public class Battery extends LapTop {@Overridepublic void charge() {System.out.println("电源供电中。");}
}
四、Power类
package lapTop;public class Power {public LapTop chargeWay(String PowerSupply) {LapTop l=null;if(PowerSupply.equals("是")) {l=new AcPower();}else {l=new Battery();}return l;}public void charge(LapTop charge) {charge.charge();}
}
五、TestLapTop类
package lapTop;import java.util.Scanner;public class TestLapTop {public static void main(String[] args) {Scanner sc=new Scanner(System.in);Power p=new Power();System.out.print("是否插入电源:");String PowerSupply=sc.next();LapTop i=p.chargeWay(PowerSupply);p.charge(i);}
}