我想问问这几个方法怎么写啊,没有思路了,应该用到哪方面的知识,谢谢啦
这里主要考察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;
}
}
}
父类,应该是继承吧,Car和Bus都继承Vehicle,然后重写基类成员,成员方法的功能不是也给出了吗
用到了继承重写方法知识,还有小小的计算,可用if语句,字符串等等,参数类型