有些知識(shí)點(diǎn)在這課程中沒(méi)有體現(xiàn),需要翻資料慢慢體會(huì)
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) {
//對(duì)象數(shù)組
Vehicle veh1[]= {
new FamilyCar("奧迪",230,5),
new FamilyCar("寶馬",290,5),
new FamilyCar("奔馳",350,7),
new Trunk("沃爾沃",430,5),
new Trunk("東風(fēng)",590,6),
new Trunk("長(zhǎng)安",350,4),
new Pickup("沃爾沃",330,2,3),
new Pickup("東風(fēng)",290,2,4),
new Pickup("長(zhǎng)安",190,2,2)
};
//判斷顯示清單
System.out.println("歡迎使用噠噠租車(chē)系統(tǒng)");
System.out.println("您是否要租車(chē):1是0否");
Scanner sc=new Scanner(System.in);
int input=sc.nextInt();
if(input==1) {
System.out.println("歡迎使用噠噠租車(chē)系統(tǒng),您可租車(chē)的類(lèi)型及其價(jià)目表:");
System.out.println("序號(hào)\t汽車(chē)名稱\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("退出系統(tǒng),謝謝!");
System.exit(0);
};
//輸入
System.out.println("請(qǐng)輸入您要租車(chē)的數(shù)量:");
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;
//創(chuàng)建數(shù)組存放篩選值
Vehicle[] sumPersonCap=new Vehicle[carAmount];
Vehicle[] sumCagoCap=new Vehicle[carAmount];
for(int j=1;j<=carAmount;j++) {
System.out.println("請(qǐng)輸入第"+j+"輛車(chē)的序號(hào)");
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];
}
}
//輸出結(jié)果
System.out.println("***請(qǐng)輸入租車(chē)的天數(shù):");
Scanner input3=new Scanner(System.in);
int rentDay=input3.nextInt();
totalPrice*=rentDay;
System.out.println("***可載人的車(chē)有:");
for(int k=0;k<p;k++) {
System.out.print(sumPersonCap[k].getName()+"\t");
}
System.out.println("共載人:"+totalPersonCap+"人");
System.out.println("***可載貨的車(chē)有:");
for(int l=0;l<q;l++) {
System.out.print(sumCagoCap[l].getName()+"\t");
}
System.out.println("共載貨:"+totalCagoCap+"噸");
System.out.println("***租車(chē)的總價(jià)格:"+totalPrice+"元");
}
}
2020-01-19
大神好,代碼里面的 name 和rent 為何是public 不是private的呢?
2019-12-26
大神?。。?!