慕前端5148996
2019-11-21 16:01
public abstract class Vehicle {
public String name;
public int rent;
public Vehicle() {
}
public Vehicle(String name,int rent) {
this.name=name;
this.rent=rent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRent() {
return rent;
}
public void setRent(int rent) {
this.rent = rent;
}
public abstract String displayCar();
}
public class FamilyCar extends Vehicle{
private int personsCap;
public FamilyCar(String name,int rent,int personsCap) {
this.name=name;
this.rent=rent;
this.personsCap=personsCap;
}
public int getPersonsCap() {
return personsCap;
}
public void setPersonsCap(int personsCap) {
this.personsCap=personsCap;
}
public String displayCar() {
return "\t"+this.name+"\t"+this.rent+"元/天"+"\t\t"+"载人:"+this.personsCap+"人";
}
}
public class Trunk extends Vehicle{
private int cagoCap;
public Trunk(String name,int rent,int cagoCap) {
this.name=name;
this.rent=rent;
this.cagoCap=cagoCap;
}
public int getCagoCap() {
return cagoCap;
}
public void setCagoCap(int cagoCap) {
this.cagoCap = cagoCap;
}
@Override
public String displayCar() {
// TODO Auto-generated method stub
return "\t"+this.name+"\t"+this.rent+"元/天"+"\t\t"+"载货:"+this.cagoCap+"吨";
}
}
public class Pickup extends Vehicle{
private int personsCap;
private int cagoCap;
public Pickup(String name,int rent,int personsCap,int cagoCap) {
this.name=name;
this.rent=rent;
this.personsCap=personsCap;
this.cagoCap=cagoCap;
}
public int getPersonsCap() {
return personsCap;
}
public void setPersonsCap(int personsCap) {
this.personsCap = personsCap;
}
public int getCagoCap() {
return cagoCap;
}
public void setCagoCap(int cagoCap) {
this.cagoCap = cagoCap;
}
@Override
public String displayCar() {
// TODO Auto-generated method stub
return "\t"+this.name+"\t"+this.rent+"元/天"+"\t\t"+"载人:"+this.personsCap+"人,"+"载货:"+this.cagoCap+"吨";
}
}
import java.util.Scanner;
public class Initial {
public static void main(String[] args) {
//对象数组
Vehicle veh1[]= {
new FamilyCar("奥迪",230,5),
new FamilyCar("宝马",290,5),
new FamilyCar("奔驰",350,7),
new Trunk("沃尔沃",430,5),
new Trunk("东风",590,6),
new Trunk("长安",350,4),
new Pickup("沃尔沃",330,2,3),
new Pickup("东风",290,2,4),
new Pickup("长安",190,2,2)
};
//判断显示清单
System.out.println("欢迎使用哒哒租车系统");
System.out.println("您是否要租车:1是0否");
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
if(input==1) {
System.out.println("欢迎使用哒哒租车系统,您可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t\t容量");
for(int i=0;i<veh1.length;i++) {
int count =i+1;
System.out.println(count+veh1[i].displayCar());
}
}
else {
System.out.println("退出系统,谢谢!");
System.exit(0);
};
//输入
System.out.println("请输入您要租车的数量:");
Scanner input1=new Scanner(System.in);
int carAmount=input1.nextInt();
int totalPrice=0;
int totalPersonCap=0;
int totalCagoCap=0;
int p=0;
int q=0;
//创建数组存放筛选值
Vehicle[] sumPersonCap=new Vehicle[carAmount];
Vehicle[] sumCagoCap=new Vehicle[carAmount];
for(int j=1;j<=carAmount;j++) {
System.out.println("请输入第"+j+"辆车的序号");
Scanner input2=new Scanner(System.in);
int carNum=input2.nextInt();
totalPrice+=veh1[carNum-1].getRent();
if(veh1[carNum-1] instanceof FamilyCar) {
totalPersonCap+=((FamilyCar)veh1[carNum-1]).getPersonsCap();
sumPersonCap[p++]=veh1[carNum-1];
}
//
if(veh1[carNum-1] instanceof Trunk) {
totalCagoCap+=((Trunk)veh1[carNum-1]).getCagoCap();
sumCagoCap[q++]=veh1[carNum-1];
}
if(veh1[carNum-1] instanceof Pickup) {
totalPersonCap+=((Pickup)veh1[carNum-1]).getPersonsCap();
totalCagoCap+=((Pickup)veh1[carNum-1]).getCagoCap();
sumPersonCap[p++]=veh1[carNum-1];
sumCagoCap[q++]=veh1[carNum-1];
}
}
//输出结果
System.out.println("***请输入租车的天数:");
Scanner input3=new Scanner(System.in);
int rentDay=input3.nextInt();
totalPrice*=rentDay;
System.out.println("***可载人的车有:");
for(int k=0;k<p;k++) {
System.out.print(sumPersonCap[k].getName()+"\t");
}
System.out.println("共载人:"+totalPersonCap+"人");
System.out.println("***可载货的车有:");
for(int l=0;l<q;l++) {
System.out.print(sumCagoCap[l].getName()+"\t");
}
System.out.println("共载货:"+totalCagoCap+"吨");
System.out.println("***租车的总价格:"+totalPrice+"元");
}
}
大神好,代码里面的 name 和rent 为何是public 不是private的呢?
大神啊!!!
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题