磕磕絆絆的,一知半解的寫了出來,感覺比你們寫的要繁瑣很多啊
父類
package com.imooc.www;
public class Che {
private int zaike;
private int zaihuo;
private int getZaike() {
return zaike;
}
public void setZaike(int zaike) {
this.zaike = zaike;
}
public int getZaihuo() {
return zaihuo;
}
public void setZaihuo(int zaihuo) {
this.zaihuo = zaihuo;
}
private int xuhao;
private int jiage;
private String name;
public int getXuhao() {
return xuhao;
}
public void setXuhao(int xuhao) {
this.xuhao = xuhao;
}
public int getJiage() {
return jiage;
}
public void setJiage(int jiage) {
this.jiage = jiage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
子類1
package com.imooc.www;
public class Keche extends Che {
@Override
public String toString() {
return ?getXuhao()+"\t"+getName()+"\t"+getJiage()+"\t"+getZaike()+"人";
}
public Keche(int xuhao,String name,int jiage,int zaike){
this.setZaike(zaike);
this.setXuhao(xuhao);
this.setJiage(jiage);
this.setName(name);
}
}
子類2
package com.imooc.www;
public class Huoche extends Che {
@Override
public String toString() {
return ?getXuhao()+"\t"+getName()+"\t"+getJiage()+"\t"+getZaihuo()+"噸";
}
public Huoche(int xuhao,String name,int jiage,int zaihuo){
this.setJiage(jiage);
this.setName(name);
this.setXuhao(xuhao);
this.setZaihuo(zaihuo);
}
}
子類3
package com.imooc.www;
public class Pika extends Che {
@Override
public String toString() {
return ?getXuhao()+"\t"+getName()+"\t"+getJiage()+"\t"+getZaike()+"人"+getZaihuo()+"噸";
}
public Pika(int xuhao,String name,int jiage,int zaike,int zaihuo){
this.setJiage(jiage);
this.setName(name);
this.setXuhao(xuhao);
this.setZaihuo(zaihuo);
this.setZaike(zaike);
}
}
測試類
package com.imooc.www;
import java.util.Scanner;
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
?Che [ ] whatChe={new Keche(1,"奧迪A4",500,4),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new Keche(2,"馬自達6",400,4),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new Pika(3,"皮卡雪6",450,4,2),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new Keche(4," 金龍 ",800,20),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new Huoche(5,"松花江",400,4),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?new Huoche(6,"依維柯",1000,20)};
? ? ? ? System.out.println("歡迎您使用答答租車系統(tǒng)!");
? ? ? ???System.out.println("您是否要租車?1 ?是 ? 2 ?否");
? ? ? ???Scanner shuru1=new Scanner(System.in);
? ? ? ???int a=shuru1.nextInt();
? ? ? ? ? ???if(a==1){
? ? ? ? ? ? ? ? ? ? ? ? System.out.println("您可租車的類型及其價目表:");
? ? ? ? ? ? ? ? ? ? ? ? System.out.println("序號"+"\t"+"汽車名稱"+"\t"+"租金"+"\t"+"容量");
?
?? ? ? ? ? ?for(int i=0;i<=whatChe.length-1;i++){
? ? ? ? ? ? System.out.println(whatChe[i]); ? //重寫了toString方法,輸出了值而不是那個碼
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? System.out.println("請輸入想要租車的數(shù)量:");
? ? ? ? ? ? ???Scanner shuru2=new Scanner(System.in);
?? ? ? ? ? ? ??int b=shuru2.nextInt();
? ? ? ? ? ? ???Che thische[]=new Che[b]; ? ? ? ??
? ? ? ? ? ? ???int tianjia = 0;//每天的價格
?? ? ? ? ? ? ??for(int j=1;j<=b;j++){
? ? ? ? ? ? ???System.out.println("請輸入第"+j+"輛車的序號");
? ? ? ? ? ? ???Scanner shuru3=new Scanner(System.in);
?? ? ? ? ? ? ??int c=shuru3.nextInt();
?
? ? ? ? ? ? ???tianjia=tianjia+ whatChe[c-1].getJiage();
?? ? ? ? ? ? ??thische[j-1]=whatChe[c-1];
? ??
?}
? ??
?? ? ? ? ? ? ??System.out.println("請輸入租車天數(shù)");
?? ? ? ? ? ? ??Scanner shuru4=new Scanner(System.in);
? ? ? ? ? ? ???int d=shuru4.nextInt();
? ? ? ? ? ? ???System.out.println("您的賬單:");
? ? ? ? ? ? ???System.out.println("可載人的車有:");
? ? ? ? ? ? ???int renshu = 0; ? ? ? ? ? ? ? ? ? ? ? //不賦值為什么不行?
?? ? ? ? ? ? ??for(int y=0;y<thische.length;y++){
? ? ? ? ? ? ???if(thische[y].getZaike()!=0){
? ? ? ? ? ? ???System.out.print(thische[y].getName()+"\t");
? ? ? ? ? ? ???renshu=thische[y].getZaike()+renshu;
? ? ? ? ? ? ???}
? ? ? ? ? ? ???}
?? ? ? ? ? ? ??System.out.println("共載人:"+renshu+"人");
?? ? ? ? ? ? ??System.out.println("可載貨的車有:");
? ? ? ? ? ? ???int huoshu = 0;
? ? ? ? ? ? ???for(int k=0;k<thische.length;k++){
? ? ? ? ? ? ???if(thische[k].getZaihuo()!=0){
? ? ? ? ? ? ???System.out.print(thische[k].getName()+"\t");
? ? ? ? ? ? ???huoshu=thische[k].getZaihuo()+huoshu;
?}
?}
?? ? ? ? ? ? ??System.out.println("共載貨"+huoshu+"噸");
?? ? ? ? ? ? ??System.out.println("租車總價格:"+d*tianjia);
? ? ? ? ? ? ???}else if(a==2){
? ? ? ? ? ? ???System.out.println("歡迎您下次使用");
? ? ? ? ? ? ???}else{
?? ? ? ? ? ? ??System.out.println("您的輸入有誤");
?}
}
}
2017-05-26
哈哈哈
2017-05-20
我懷疑你腦子進水了