回来 别走 2022-06-10 21:07 采纳率: 71.4%
浏览 25
已结题

我想问问这几个方法是什么意思啊

我想问问这几个方法怎么写啊,没有思路了,应该用到哪方面的知识,谢谢啦

img

  • 写回答

3条回答 默认 最新

  • 陈脩 2022-06-10 21:46
    关注

    这里主要考察Java继承,子类和父类是is-a, Car和Bus都是交通工具

    public class VehicleTest {
        public static void main(String[] args) {
            Car bmw = new Car();
            bmw.setBrand("宝马");
            bmw.setType("750");
            System.out.println(bmw.info() + " : " + bmw.calRent(1));
    
            Bus bus=new Bus();
            bus.setBrand("大众");
            bus.setSeatNum(8);
    
            System.out.println(bus.info()+" : "+bus.calRent(3));
        }
    
        public static class Vehicle {
    
            private String no;
    
            private String brand;
    
            public String getNo() {
                return no;
            }
    
            public void setNo(String no) {
                this.no = no;
            }
    
            public String getBrand() {
                return brand;
            }
    
            public void setBrand(String brand) {
                this.brand = brand;
            }
        }
    
        public static class Car extends Vehicle {
            private static Map<String, Integer> rentMap = new HashMap<>();
    
            static {
                rentMap.put("宝马750", 700);
                rentMap.put("大众polo", 400);
                rentMap.put("大众golf", 00);
            }
    
            private String type;
    
            public String info() {
                String str = getBrand() + getType();
                return str;
            }
    
            public int calRent(int days) {
                return rentMap.getOrDefault(info(), 0) * days;
            }
    
            public String getType() {
                return type;
            }
    
            public void setType(String type) {
                this.type = type;
            }
        }
    
        public static class Bus extends Vehicle {
            private static Map<String, Integer> rentMap = new HashMap<>();
    
            static {
                rentMap.put("<=20座", 800);
                rentMap.put(">20", 1200);
            }
    
            private int seatNum;
    
            private String info() {
                String info = seatNum <= 20 ? "<=20座" : ">20";
                return info;
            }
    
            public int calRent(int days) {
                return rentMap.getOrDefault(info(), 0) * days;
            }
    
            public int getSeatNum() {
                return seatNum;
            }
    
            public void setSeatNum(int seatNum) {
                this.seatNum = seatNum;
            }
        }
    }
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月19日
  • 已采纳回答 6月11日
  • 创建了问题 6月10日

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统