交作业!大家来交流交流,提提意见
测试类
import java.util.Scanner; public class Play { public static void main(String[] args) { Car car1 = new Truck("货车1号", 100, 4); Car car2 = new Truck("货车2号", 200, 15); Car car3 = new MannedCar("客车1号", 300, 15); Car car4 = new MannedCar("客车2号", 400, 4); Car car5 = new PickupTruck("皮卡车1号", 400, 2,4); Car car6 = new PickupTruck("皮卡车2号", 500, 3,2); Car[] cars = new Car[6]; cars[0] = car1; cars[1] = car2; cars[2] = car3; cars[3] = car4; cars[4] = car5; cars[5] = car6; System.out.println("欢迎使用嗒嗒租车系统:"); System.out.println("您是否要租车:1是 0否"); Scanner scanner = new Scanner ( System.in ); int yes_no = scanner.nextInt (); if (yes_no == 1) { System.out.println ( "您可以租车的类型及价目表:" ); System.out.println ( "序号 名称 租金 容量" ); for (int i = 0; i < cars.length; i++) { System.out.print ( i + 1 + ". " ); cars[i].show (); } //eg1.foreeach遍历 /*for (Car car : cars) { car.show (); }*/ //eg2.while遍历 /*int i = 0; while(i<cars.length){ cars[i].show(); i++; }*/ System.out.print ( "请输入您要租汽车的数量: " ); int lease = scanner.nextInt (); //生成储存租借汽车的数组 Car[] zcar = new Car[lease];//zcar数组长度取决于用户输入的lease变量值 for (int n = 1; n < lease + 1; n++) { System.out.print ( "请输入第" + n + "车的序列号: " ); int serial = scanner.nextInt (); zcar[n-1] = cars[serial-1];//把cars数组中[serial-1]传入zcar数组中[n-1]去 } //测试zcar(租借车辆数组) /*for (int i = 0; i<lease; i++){ zcar[i].show (); }*/ //day变量存储输入天数 System.out.print ( "请输入租车的天数: " ); int day = scanner.nextInt (); //遍历筛选载人汽车 System.out.println ("***可载人的汽车有: "); for (int i = 0; i<lease; i++){ if (zcar[i].manned != 0){ System.out.print (zcar[i].name + " "); } } //遍历筛选载货汽车 System.out.println (); System.out.println ("***可载货的汽车有: "); for (int i = 0; i<lease; i++){ if (zcar[i].goods != 0){ System.out.print (zcar[i].name + " "); } } //计算总价格 System.out.println (); System.out.println ("***租车总价格为: "); int moneny = 0; for (int i = 0; i<lease;i++){ moneny += zcar[i].price * day; } System.out.println (moneny + "元"); }else { System.out.println ("好的,再见"); System.exit ( 0 ); } } }
Car类(父类)
public class Car { public String name; public int price; public int goods; public int manned; public void show(){ } }
Truck类(子类)
public class Truck extends Car{ //private int goods; public Truck(String newName,int newPrice,int newgoods){ name = newName; price = newPrice; goods = newgoods; } public void show(){ System.out.println ( name + " " + price + "元/天 " +"载货:" + goods + "吨" ); } //没用的 /*public int getGoods() { return goods; } public void setGoods(int goods) { this.goods = goods; }*/ }
MannedCar类(子类)
public class MannedCar extends Car{ //private int manned; public MannedCar(String newName,int newPrice,int newManned){ name = newName; price = newPrice; manned = newManned; } public void show(){ System.out.println ( name + " " + price + "元/天 " +"载人:" + manned + "人" ); } public int getManned() { return manned; } public void setManned(int manned) { this.manned = manned; } }
PickupTruck类(子类)
public class PickupTruck extends Car{ //private int goods; //private int manned; public PickupTruck(String newName,int newPrice,int newGoods,int newManned){ name = newName; price = newPrice; goods = newGoods; manned = newManned; } public void show(){ System.out.println ( name + " " + price + "元/天 " +"载人:" + manned + "人" + " 载货:" + goods + "吨"); } //没用的 /*public int getGoods() { return goods; } public int getManned() { return manned; } public void setGoods(int goods) { this.goods = goods; } public void setManned(int manned) { this.manned = manned; }*/ }
这个abstract类到底怎么用,改来改去都不行,还有其他的地方有没有什么错误哈,互相交流交流