zoukankan      html  css  js  c++  java
  • 用多态写一个租赁公司的出租信息

    父类: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();        

         }

    }

  • 相关阅读:
    加密web.config
    SQL FOR XML
    SQL语句中拆分字段
    Units specified don't exist SHSUCDX can't install
    SQLSERVER与C#中数据类型的对应关系
    使用 FOR XML PATH 產生 XML 格式時,遇到 NULL 該如何處理?
    T_SQL的 FOR XML PATH 用法
    T-SQL with关键字
    Sqlserver获取行号
    win10以太网没有有效的ip配置
  • 原文地址:https://www.cnblogs.com/wangxinqiang1995/p/5750898.html
Copyright © 2011-2022 走看看