- package demo.demo;
- import demo.demo.Car;
- import demo.demo.CarDemo;
- import demo.demo.NoValueException;
- import demo.demo.NoYouException;
- import demo.demo.SWC;
- /**
- * 一个复习抽象类的好题 与大家一起练习:
- 需求:1.先描述汽车的抽象类,该类有汽车品牌、汽车用油型号、汽车当前油量等几个属性,
- 同时定义一个加油普通函数和一个开车的抽象方法
- 2.定义一个商务小车类继承汽车抽象类,
- 商务小车类有汽车颜色、汽车价格等新属性(其他熟悉可自己添加一两个)。
- 同时在该类中重写加油、开车两个方法。要求在加油函数中增加汽车当前油量,在开车方法中减少汽车当前油量,并实时打印出当前油量。
- * 油箱可装200升
- */
- abstract class Car {
- private String pingPai,xingHao;
- public String getPingPai() {
- return pingPai;
- }
- public void setPingPai(String pingPai) {
- this.pingPai = pingPai;
- }
- public String getXingHao() {
- return xingHao;
- }
- public void setXingHao(String xingHao) {
- this.xingHao = xingHao;
- }
- public int getYouLiang() {
- return youLiang;
- }
- public void setYouLiang(int youLiang) {
- this.youLiang = youLiang;
- }
- private int youLiang;
- public void jiaYou(int num){
- youLiang+=num;
- }
- public abstract void run(int gongLi,int yiPaoGongLi)throws NoYouException;
- public Car(){
-
- }
- public Car(String pingPai,String xingHao){
- this.setPingPai(pingPai);
- this.setXingHao(xingHao);
- this.setYouLiang(100);
- }
- }
- class SWC extends Car{
- private String color;
- private int pay,runGongli;
- public SWC(String pingPai,String xingHao,String color,int pay){
- super(pingPai,xingHao);
- this.setColor(color);
- this.setPay(pay);
- }
- public SWC(){
-
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public int getPay() {
- return pay;
- }
- public void setPay(int pay) {
- this.pay = pay;
- }
- public void jiaYou(int num){
- this.setYouLiang(this.getYouLiang()+num);
- System.out.println("已加油"+num+"升");
- }
- public void run(int gongLi,int yiPaoGongLi)throws NoYouException{
- for(int i=0;i<gongLi;i++){
- if(this.getYouLiang()<8){
- throw new NoYouException("车油量不足,已跑公里"+yiPaoGongLi);
- }
- //没跑一公里耗费8升油
- this.setYouLiang(this.getYouLiang()-8);
- runGongli++;
- yiPaoGongLi++;
- }
- System.out.print(this.getPay()+"商务车:"+this.getColor()+this.getPingPai()+this.getXingHao()
- +"已跑"+runGongli+"公里"+"还剩"+this.getYouLiang()+"升油");
- }
- }
- class NoYouException extends Exception{
- public NoYouException(){
- super("车油量不足");
- }
- public NoYouException(String message){
- super(message);
- }
- }
- class NoValueException extends RuntimeException{
- public NoValueException(){
- super("已跑距离大于为跑距离");
- }
- }
- public class CarDemo{
- SWC car=null;
- public static void main(String[] args) {
- new CarDemo().run(30,0);
- }
- public void run(int gongLi,int yiPaoGongLi){
- if(yiPaoGongLi>gongLi){
- throw new NoValueException();
- }
- if(car==null){
- car=new SWC("兰博基尼","LF23","黑色",1000000);
- }
- car.jiaYou(100);
- try {
- car.run(gongLi, yiPaoGongLi);
- } catch (NoYouException e) {
- String str=e.getMessage();
- str=str.substring(str.indexOf("里")+1, str.length());
- yiPaoGongLi=Integer.parseInt(str);
- // System.out.println(yiPaoGongLi);
- gongLi-=yiPaoGongLi;
- yiPaoGongLi=0;
- run(gongLi,yiPaoGongLi);
- }
- }
- }
复制代码 |