求大神:載人車車輛和載貨車輛怎么分開(kāi)輸出,我結(jié)合別人的只能一塊輸出
package com.imooc;
public class Car {
? ? String name;//定義一個(gè)名字
? ? int price;//定義租金
? ? int number;//載人數(shù)
? ? int weight;//載貨數(shù)
}
package com.imooc;
public class Manned extends Car {
? ? ? ? Manned(String name,int price,int number){
? ? this.name=name;
? ? this.price=price;
? ? this.number=number;
? ? }
}
package com.imooc;
public class Cargo extends Car {
Cargo(String name,int price,int weight){
? ? this.name=name;
? ? this.price=price;
? ? this.weight=weight;
}
}
package com.imooc;
public class Pickup extends Car {
Pickup(String name,int price,int number,int weight){
? ? this.name=name;
? ? this.price=price;
? ? this.number=number;
? ? this.weight=weight;
}
}
package com.imooc;
import java.util.Scanner;
public class Test {
?Car[] rent=new Car[100];//定義一個(gè)大數(shù)組
Car cars[]={new Manned("江淮客車",300,40),new Cargo("陜汽奧龍",600,50),new Manned("奔馳",700,5),new Cargo("一汽",500,5),new Pickup("皮卡",200,4,1)};
int count;//數(shù)量
? ? int day;//天數(shù)
? ? int money;//價(jià)格
public static void main(String[] args) {
// TODO Auto-generated method stub
? ?Test hello=new Test();
?Scanner input =new Scanner(System.in);
?System.out.println("歡迎您使用答答租車系統(tǒng):");
?System.out.println("您是否要租車:1是2否");
?int inputNumber=input.nextInt();
?if(inputNumber==1){
?System.out.println("您可租車的類型及其價(jià)目表:");
?System.out.println("序號(hào) ?汽車名稱 ? ?租金 ? ? ? ? ? ? ? ?容量:");
?System.out.println("1. 江淮客車 ? ? 300/天 ? ? ? 載人:40人" ?);
?System.out.println("2. 陜汽奧龍 ? ? 600/天 ? ? ?載貨:50噸");
?System.out.println("3. 奔馳 ? ? ? ? ? ? ?700/天 ? ? ?載人:5人");
?System.out.println("4. 一汽 ? ? ? ? ? ? ?500/天 ? ? ?載貨5噸");
?System.out.println("5. 皮卡 ? ? ? ? ? ? ?200/天 ? ? ?載人:4人,載貨:1噸");
?}
?if(inputNumber==2){
?input.close();
?System.exit(-1);
?}
?System.out.println("請(qǐng)輸入您要租車的數(shù)量");
?int carNumber=input.nextInt();
?for(int i=0;i<carNumber;i++){
?System.out.println("請(qǐng)輸入第"+(i+1)+"輛車的序號(hào)");
?int a=input.nextInt();
?while (a < 1 || a > 6) {
? ? ? ? ? ? ? ? ?System.out.println("您輸入的型號(hào)有誤,請(qǐng)重新輸入選擇車型");?
? ? ? ? ? ? ? ? ?int b = input.nextInt();
a=b;
?}
? ? hello.money += hello.cars[a-1].price;
? ? hello.rent[i]= hello.cars[a-1];
?}
? ? ? ? System.out.println("請(qǐng)輸入租車天數(shù)");
? ? ? ? int day=input.nextInt();
? ? ? ? hello.money*=day;
? ? ? ? System.out.println("您的租車賬單為:");
int mannedNumber=0;
int cargoNumber=0;
? ? ? ? for(int i=0;i<carNumber;i++){
? ? ? ? System.out.print(hello.rent[i].name + " ");
? ? ? ? mannedNumber+=hello.rent[i].number;
? ? ? ? cargoNumber+=hello.rent[i].weight;
? ? ? ? }
? ? ? ? System.out.println("您的租車載人數(shù)量" + mannedNumber + "人");
? ? ? ? System.out.println("您的租車載貨量" + cargoNumber + "噸");
? ? ? ? System.out.println("您的租車總費(fèi)用:" + hello.money);
}
}
2018-03-10
可以引入接口概念。java父類只能單繼承,但接口可以多個(gè)繼承??梢钥紤]在你的代碼Car類中僅僅只加入name和price兩個(gè)變量,創(chuàng)建載人Human和載物Cargo的兩個(gè)接口,載人客車?yán)^承Car同時(shí)繼承Human接口,貨車?yán)^承Car的同時(shí),繼承Cargo接口,皮卡車?yán)^承Car并同時(shí)繼承Human和Cargo兩個(gè)接口,這樣可以實(shí)現(xiàn)你所說(shuō)的分別輸出。