• 一个面向对象的继承,抽象,私有属性和重写方法的综合实例


     //要求,按照车子类型天数计算租金

    文件一,机动车类:

    public abstract class Moto {
        public String nm;
        public String color;
        public String brand;
        public String mil;
    
        /**
         * 计算租金
         *
         * @param days
         * @return
         */
        public abstract int Calrentint(int days);
    }

    文件二,小轿车类:

    public class Car extends Moto {
        public final static int Type1 = 1;
        public final static int Type2 = 2;
        public final static int Type3 = 3;
    
        private int type;
    
        public int getType() {
            return type;
        }
    
        public void setType(int type) {
            this.type = type;
        }
    
        @Override
        /**
         * 按照类型计算租金
         */
        public int Calrentint(int days) {
            int money = 0;
            switch (this.type) {
                case Type1:
                    money = 600 * days;
                    break;
                case Type2:
                    money = 400 * days;
                    break;
                case Type3:
                    money = 300 * days;
                    break;
            }
            return money;
        }
    }

    文件三,客车类:

    public class Bus extends Moto {
        public int seatCount;
    
        public int getSeatCount() {
            return seatCount;
        }
    
        public void setSeatCount(int seatCount) {
            this.seatCount = seatCount;
        }
    
        @Override
        /**
         * 按照座位数计算租金
         */
        public int Calrentint(int days) {
            int money = 0;
            if (this.seatCount <= 16) {
                money = days * 800;
            } else if (this.seatCount > 16) {
                money = days * 1600;
            }
            return money;
        }
    }

    文件四,主方法:

    import java.util.Scanner;
    public class Ui {
        public static void main(String[] args) {
            int money = 0;
            System.out.println("请选择租车类型");
            Scanner input = new Scanner(System.in);
            System.out.println("请选择1,出租车2.客车");
            int chose = input.nextInt();
            switch (chose) {
                case 1:
                    System.out.println("请选择出租车类型(1.别克商务舱GL8 2.宝马550i 3.别克林荫大道)");
                    int t = input.nextInt();
                    System.out.println("输入租车天数");
                    int d = input.nextInt();
                    Car car = new Car();
                    car.setType(t);
                    money = car.Calrentint(d);
                    break;
                case 2:
                    System.out.println("请选择座位数");
                    int bt = input.nextInt();
                    System.out.println("输入租车天数");
                    int bd = input.nextInt();
                    Bus bus = new Bus();
                    bus.setSeatCount(bt);
                    money = bus.Calrentint(bd);
                    break;
            }
            System.out.println("需要租金" + money);
        }
    }
  • 相关阅读:
    emulate sh
    postmaster.c 中的 ListenAddresses
    PostgreSQL的postmaster的fork动作验证
    NotifyMyFrontEnd 函数背后的数据缓冲区(三)
    对${ZSH_VERSION+set}的验证
    微软正准备一个简易的Rootkit清除方案 助用户打补丁 狼人:
    创新与安全:云计算的两只跷跷板 狼人:
    苹果禁止iPhone黑客访问App Store应用商店 狼人:
    春节不回家 单身留守族“拼饭”“拼玩” 狼人:
    僵尸侵入全球 袭击者或为东欧黑帮 狼人:
  • 原文地址:https://www.cnblogs.com/lwj820876312/p/7243126.html
Copyright © 2020-2023  润新知