qq_自娱自乐_14
2020-01-05 19:14
package com.car; public class Car { protected String carName; //汽车名称 protected Double rent; //租金 public Car(String carName, double rent){ this.carName = carName; this.rent = rent; } public String getCarName(){ return this.carName; } public Double getRent(){ return this.rent; } }
AManned
package com.car; /** * 载人汽车 */ public class AManned extends Car{ protected int personCount; public AManned(String carName, double rent, int personCount){ super(carName, rent); this.personCount = personCount; } public int getPersonCount(){ return this.personCount; } }
ATruck:
package com.car; /** * 货车 */ public class ATruck extends Car{ protected int productCount; public ATruck(String carName, double rent, int productCount){ super(carName, rent); this.productCount = productCount; } public int getProductCount(){ return this.productCount; } }
AMulti:
package com.car; /** * 多功能汽车 */ public class AMulti extends Car{ protected int productCount; protected int personCount; public AMulti(String carName, double rent, int personCount, int productCount){ super(carName, rent); this.productCount = productCount; this.personCount = personCount; } public int getPersonCount(){ return this.personCount; } public int getProductCount(){ return this.productCount; } }
index:
package com.car; import java.util.Scanner; public class index { public static void main(String[] args){ //引导语 System.out.println("欢迎使用租车系统"); System.out.println("您是否要租车?1-是,0-否"); Scanner input = new Scanner(System.in); int isRent = input.nextInt(); //是否租车 if (isRent == 1) { System.out.println("您可租车的类型和价目表:"); System.out.println("序号 汽车名称 租金 容量"); //声明所有汽车并显示 Car[] cars = { new AManned("奥迪",500, 4), new AManned("马自达",400, 4), new AMulti("皮卡雪",450, 4, 2), new AManned("金龙",800, 20), new ATruck("松花江", 400, 4), new ATruck("依维柯",1000, 20) }; for (int i = 0; i < cars.length; i++) { int index = i+1; if (cars[i] instanceof AManned) { //载人 AManned car = (AManned) cars[i]; System.out.println( index + " "+ car.getCarName()+ " "+ car.getRent()+ "元/天 "+ "载人:"+ car.getPersonCount()+ "人 " ); } else if(cars[i] instanceof ATruck) { //载货 ATruck car = (ATruck) cars[i]; System.out.println( index +" "+ car.getCarName()+ " " + car.getRent()+ "元/天 " + "载货:"+ car.getProductCount()+"吨" ); } else { AMulti car = (AMulti) cars[i]; System.out.println( index+ " "+ car.getCarName()+ " " + car.getRent()+ "元/天 " + "载人:"+car.getPersonCount()+ "人 "+ "载货:"+car.getProductCount()+"吨"); } } //选择要租的车,并记录下来 System.out.println("请选择要租车的数量:"); int carsCount = input.nextInt(); Car[] chooseCar = new Car[carsCount]; for (int j = 0; j < carsCount; j++) { int cycle = j+1; System.out.println("请选择第"+ cycle +"辆车:"); int carNum = input.nextInt(); chooseCar[j] = cars[carNum-1]; } //选择租车天数,并计算出租金 System.out.println("请输入要租的天数:"); int days = input.nextInt(); double singleSum = 0; double sum = 0; for (Car aCar: chooseCar) { singleSum += aCar.getRent(); } sum = singleSum * days; System.out.println("您的租金是:"+sum); } } }
for
(Car aCar: chooseCar)
这里的Car aCar是什么,这个循环怎么看,这知识点不会啊,“:”是什么意思啊,大神求解答
很强,看完之后深受启发,感谢
楼主,我想问一下,你那个强制引用类型转换的意义在哪?我在想不用强制类型转换,用cars[i].方法这样形式去操作也行啊,麻烦解答一下谢谢。
厉害?!
厉害?!
感觉这一季的大多知识都用上了,就是没有用上抽象的知识。
楼主,为啥我输出的汽车名称都是null啊
我找了一晚上都没找到错哪了
请问楼主
Car[] cars是什么用法?数组不是得数据类型 【】数组名吗?Car不是父类么?
666
太强了
AManned car = (AManned) cars[i];
楼主,这个是干什么的啊,没看懂!
是调用数组里的对象吗
大佬,请收下我的膝盖!
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题