我想问问这几个方法怎么写啊,没有思路了,应该用到哪方面的知识,谢谢啦
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; } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥20 测距传感器数据手册i2c
- ¥15 RPA正常跑,cmd输入cookies跑不出来
- ¥15 求帮我调试一下freefem代码
- ¥15 matlab代码解决,怎么运行
- ¥15 R语言Rstudio突然无法启动
- ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
- ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
- ¥15 用windows做服务的同志有吗
- ¥60 求一个简单的网页(标签-安全|关键词-上传)
- ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法