慕姐0352535
2019-05-08 15:27
package project.Car; public abstract class Car { String name; double price; int person, cargo; int getCargo() { return 0; } int getPerson() { return 0; } }
public class CargoCar extends Car{ //装货 private int cargo; public CargoCar(String name, double price, int cargo) { this.name = name; this.price = price; this.setCargo(cargo); } public int getCargo() { return cargo; } public void setCargo(int cargo) { this.cargo = cargo; } } public class PassengerCar extends Car { //载人 private int person; public PassengerCar(String name, double price, int person) { this.name = name; this.price = price; this.setPerson(person); } public int getPerson() { return person; } public void setPerson(int person) { this.person = person; } } public class PickupCar extends Car{ //皮卡 private int person; private int cargo; public PickupCar(String name, double price, int person, int cargo) { this.name = name; this.price = price; this.person = person; this.cargo = cargo; } public int getPerson() { return person; } public void setPerson(int person) { this.person = person; } public int getCargo() { return cargo; } public void setCargo(int cargo) { this.cargo = cargo; } } package project.Car; import java.util.Scanner; public class Car_Rent { static double Price, num_Cargo; //Price为总价格, num_Cargo为总货物量 static int num_Person; //num_Person为可载总人数 public static void main(String[] args) { if (askFirst() == 1) { Car[] car = {new PassengerCar("奥迪A4", 500, 4), new PassengerCar("马自达6", 400 , 4), new PickupCar("皮卡雪6", 450, 4, 2),new PassengerCar("金龙", 800, 20), new CargoCar("松花江", 400, 4), new CargoCar("依维柯", 1000, 20) }; show(car); askSecond(car); } } static int askFirst() { //判断是否租车 System.out.println("欢迎使用租车系统:"); System.out.println("您是否要租车:1是 0否"); Scanner input = new Scanner(System.in); int judge = input.nextInt(); return judge; } static void show(Car[] car) { //展示价目表,\t制表符,横向跳8个空格 System.out.println("您可租车的类型及其价目表:\n序号\t汽车名称\t\t租车\t\t容量"); for(int i = 0; i < car.length; i++) { if (car[i].person > 0 && car[i].cargo > 0) { System.out.println((i+1)+".\t"+car[i].name+"\t\t"+car[i].price+"元/天\t "+ "载人:"+car[i].person+"人 "+"载货:"+car[i].cargo+"吨"); } else if (car[i].person > 0) { System.out.println((i+1)+".\t"+car[i].name+"\t\t"+car[i].price+"元/天\t"+ "载人:"+car[i].person+"人 "); } else { System.out.println((i+1)+".\t"+car[i].name+"\t\t"+car[i].price+"元/天\t"+ "载货:"+car[i].cargo+"吨"); } } } static void askSecond(Car[] car) { System.out.println("请输入您要租汽车的数量:"); Scanner input = new Scanner(System.in); int num = input.nextInt(); //num为租车数量 int[] recode = new int[num]; //recode数组记录所租车辆的编号,tag是该数组编号 int tag = 0; for (int i = 1; i <= num; i++){ //输入所租车辆序号,更新总价格和载货量,载人数等数据 System.out.println("请输入第"+i+"辆车的序号:"); int code = input.nextInt(); recode[tag++] = code-1; Price += car[code-1].price; num_Cargo += car[code-1].getCargo(); num_Person += car[code-1].getPerson(); } System.out.println("请输入租车天数:"); int days = input.nextInt(); Price *= days; System.out.println("您的账单:\n***可载人的车有:"); for(int temp = 0; temp < tag; temp++) { if (car[recode[temp]].getPerson() > 0) { //输出能载人的车的name,下面同理 System.out.print(car[recode[temp]].name+" "); } } System.out.println("共载人:"+num_Person+"人"); System.out.println("***载货的车有:"); for(int temp = 0; temp < tag; temp++) { if (car[recode[temp]].getCargo() > 0) { System.out.print(car[recode[temp]].name+" "); } } System.out.println("共载货:"+num_Cargo+"吨"); System.out.println("***租车总价格:"+Price+"元"); } }
你前面在CargoCar类,PassengerCar类和PickupCar类里面把person和cargo设置为private,在下面的main方法里面又直接调用了这两个属性而不是通过get方法来获取,所以输出的结果容量都是0
程序展示 载货怎么都是0啊
请教一个问题,我把方法写到主类里面了,你这是都写到主方法里面的吧?这两种方式哪个更好一点呢?
Java入门第二季 升级版
530559 学习 · 6091 问题
相似问题