• 用多态写一个租赁公司的出租信息


    父类:MotoVehicle

    public abstract class MotoVehicle {    

         String no;    

         String brand;    

         String color;    

         String mileage;    

         int days;    

         public abstract float CalcRent(int days); }

    子类1:Car

    public final class Car extends MotoVehicle {

          String type;  

          @Override  

          public float CalcRent(int days) {    

                float rent = 0;  

                if(type.equals("别克商务舱GL8")){

                       rent = 600*days;  

                }else if(type.equals("宝马550i")){

                       rent = 500*days;   

                }else if(type.equals("别克林荫大道")){

                       rent = 300*days;  

                }

                return rent;

         }

         public Car() {

                super();

         }

         public Car(String type) {  

                super();  

                this.type = type;

       }

    }

    子类2:Bus

    public final class Bus extends MotoVehicle {    

          int seatcount;

          @Override

          public float CalcRent(int days) {   

                float rent = 0;

                if(seatcount<=16){    

                      rent = 800*days;  

                }else{

                      rent = 1500*days;  

                }  

                return rent;

          }  

          public Bus() {   

                super();

          }

           public Bus(int seatcount) {  

                super();

                this.seatcount = seatcount;

           }  

    }

    测试类:MotoVehicleText

    import java.util.Scanner;

    public class MotoVehicleText {

          public static void main(String[] args) { 

               String kind;  

               float rent = 0;

               Scanner input = new Scanner(System.in);  

               System.out.println("请输入租车的种类");   

               kind = input.next();  

               if(kind.equals("car")){    

                     Car a = new Car();    

                     System.out.println("请输入租车时间:");     

                     a.days = input.nextInt();       

                     System.out.println("请输入租车的型号:");       

                     a.type = input.next();        

                     rent = a.CalcRent(a.days);   

                }else if(kind.equals("bus")){    

                     Bus b = new Bus();    

                     System.out.println("请输入租车时间:");    

                     b.days = input.nextInt();

                     System.out.println("请输入租车的座位数:");  

                     b.seatcount = input.nextInt();   

                     rent = b.CalcRent(b.days);   

               }     

               System.out.println("您的租车费用为"+rent);        

               input.close();        

         }

    }

  • 相关阅读:
    Android 7.0及以上使用OpenCL
    image_channel_data_type含义
    Valgrind.Callgrind使用
    如何在WIN10内置Ubuntu中有多个terminal
    Android: 在native中访问assets全解析
    OpenCL的buffer以及sub-buffer
    C语言程序设计(五) 选择控制结构
    C语言程序设计(三) 简单的算术运算和表达式
    C语言程序设计(二) C数据类型
    C语言程序设计(一) 为什么要学C语言
  • 原文地址:https://www.cnblogs.com/wangxinqiang1995/p/5750898.html
Copyright © 2020-2023  润新知