package?ZhuCheXiTong;
?//定義抽象車的父類
?public?abstract?class?Car?{??
??public?int?id;???????????//定義車輛序號??
??public?String?name;??????//車輛的名稱???
??public?double?price;?????//定義價格???
??public?int?busload;??????//載客量???
??public?int?weigth;???????//載重量
??public?int?getId()?{????????//封裝對象
????????return?id;
??}
??public?void?setId(int?id)?{
?? this.id?=?id;
??}
??public?String?getName()?{
????????return?name;
??}
??public?void?setName(String?name)?{
?? this.name?=?name;
??}
??public?double?getPrice()?{
?? return?price;
??}
??public?void?setPrice(double?price)?{
????????this.price?=?price;
??}
??public?int?getBusload()?{
?? return?busload;
??}
??public?void?setBusload(int?busload)?{
????????this.busload?=?busload;
??}
??public?int?getWeigth()?{
????????return?weigth;
??}
??public?void?setWeigth(int?weigth)?{
?? this.weigth?=?weigth;
??}
??public?Car(){??????????????????//無參構(gòu)造
????}?
??public?Car(int?id,String?name,double?price,int?busload,int?weigth){ ?
????this.id=id;?? ???
????this.name=name;??????????????//有參構(gòu)造 ??
????this.price=price; ???
????this.busload=busload; ??
????this.weigth=weigth;
?????}???
??public?abtract?void?show();??????//定義抽象方法}
?????//定義可以載人的車
??class?Busload?extends?Car{
??public?Busload(){ //無參構(gòu)造 }
??public?Busload(int?id,String?name,double?price,int?busload){
??//?super();
??this.id=id;??
??this.name=name;
??this.price=price;
??this.busload=busload;
??}
??public?void?show(){
??System.out.println(id+"\t"+name+"\t"+price+"元/天??"+"\t載人:"+busload+"人");
??}
??}
??//定義可以載貨的車
??class?Weigth?extends?Car{
??public?Weigth(){ }
??public?Weigth(int?id,String?name,double?price,int?weigth){
??// super();
?????this.id=id;
?????this.name=name;
?????this.price=price;
?????this.weigth=weigth;
??}
??public?void?show(){
??System.out.println(id+"\t"+name+"\t"+price+"元/天??"+"\t載貨:"+weigth+"噸");
??}
??}
??//定義即可載人也可以載貨
??class?Pick?extends?Car{
??public?Pick(){ }
??public?Pick(int?id,String?name,double?price,int?busload,int?weigth){
??super(id,name,price,busload,weigth);?????//直接使用父類的變量
??// this.id=id;
??// this.name=name;
??// this.price=price;???????
??// this.busload=busload;
??// this.weigth=weigth;
??}
??public?void?show(){
??System.out.println(id+"\t"+name+"\t"+price+"元/天??"+"\t載人:"+busload+"人"+",載貨:"+weigth+"噸");
??}
??}
??
??package?ZhuCheXiTong;
??import?java.util.Scanner;
??//測試租車系統(tǒng)
??public?class?Test?{
??public?static?void?main(String[]?args)?{
??//?TODO?Auto-generated?method?stub??????
??Car[]?car=???? ?
???{???? ?
???new?Busload(1,"奧迪A4",500,4),????
???new?Busload(2,"馬自達",400,4),????
???new?Pick(3,"皮卡雪",450,4,2),???? ?
???new?Busload(4,"金龍",800,20),???? ?
???new?Weigth(5,"松花江",400,4),???? ?
???new?Weigth(6,"依維柯",1000,20),??????????
???};????????
???double?sumprice=0;????????
???int?sumbusload=0;????????
???int?sumweigth=0;????????
???Scanner?in=new?Scanner(System.in);????????
???System.out.println("歡迎使用嗒嗒租車系統(tǒng):\n你是否要租車:1-是???0-否");????????
???int?number=in.nextInt();????????
???if(number==1){????????
???System.out.println("可供選擇的車型有:");????????
???System.out.println("序號\t汽車名稱\t租金\t\t容量");???????
????for(Car?cr:car){????????
????cr.show();?????//遍歷所有的車情況???????
????}????????????
????System.out.println("請你輸入要租汽車的數(shù)量:");???????
????int?num=in.nextInt();????????
????int?flag;??
????int?ID[]=new?int[num];??//用于存儲車的序號??????
????for(int?i=0;i<num;i++){????????
????System.out.println("請輸入第"+(i+1)+"個車輛的序號:");????????????
????flag=in.nextInt();????????
????if(flag>6||flag<0){???????????????????????
??????//防止出入超出車序號的車????????
????System.out.println("你輸入的車序號有誤!");??????????????????
????System.out.println("請輸入第"+(i+1)+"個車輛的序號:");????????????
????flag=in.nextInt();????????
????}???????? ??????? ?
????sumprice=sumprice+car[flag-1].price;??????//選了多少輛計算每輛車和的價錢???
????ID[i]=flag;???????//獲取要的車序號????
????}????????
????System.out.println("請你輸入要租汽車的天數(shù):");????????
????int?ff=in.nextInt();????????
????System.out.println("統(tǒng)計完成正在生成你的賬單\n你的賬單如下:\n***可載人的車有***");????????
????for(int?i=0;i<num;i++){????????
????if(car[ID[i]-1].busload!=0){????????
????sumbusload=sumbusload+car[ID[i]-1].busload;??????//計算能載多少人????????
????System.out.print("\t"+car[ID[i]-1].name);????????
???? }????????
???? }????????
????System.out.println("\t\t共載"+sumbusload+"人");???????
????System.out.println("***可載貨的車有***");???????
?????for(int?i=0;i<num;i++){????????
?????if(car[ID[i]-1].weigth!=0){???????//判斷只要是載貨不為0就可以載貨????????
?????sumweigth=sumweigth+car[ID[i]-1].weigth;????????//計算能載多少貨????????
?????System.out.print("\t"+car[ID[i]-1].name);????????
?????}???????
??????}???????
???????System.out.println("\t\t共載"+sumweigth+"噸");????????????????
???????System.out.println("***租出車總價格為:"+sumprice*ff);???????
???????}????????
???????else{????????
???????System.out.println("退出系統(tǒng)!希望你下次能用租車系統(tǒng)!");???????
????????}
????????}??
???????????}
2019-08-16
package com.imooc;
import com.imooc.Car;
import java.util.Scanner;
public class Home {
? //測試租車系統(tǒng)?
? public static void main(String[] args) {? ? ??
? // TODO Auto-generated method stub? ? ??
? Car[] car=? ? ??
? ?{? ? ? ? ??
? ?new Busload(1,"奧迪A4",500,4),? ? ? ??
? ?new Busload(2,"馬自達",400,4),? ? ? ? ?
? ?new Pick(3,"皮卡雪",450,4,2),? ? ? ? ? ??
? ?new Busload(4,"金龍",800,20),? ? ? ? ? ?
? ?new Weigth(5,"松花江",400,4),? ? ? ? ? ??
? ?new Weigth(6,"依維柯",1000,20),? ? ? ? ??
? ?};??
?
? ?double sumprice=0;? ? ? ??
? ?int sumbusload=0;? ? ? ??
? ?int sumweigth=0;? ? ? ??
? ?Scanner in=new Scanner(System.in);? ? ? ??
? ?System.out.println("歡迎使用嗒嗒租車系統(tǒng):\n你是否要租車:1-是? ?0-否");? ? ? ??
? ?int number=in.nextInt();? ? ? ??
? ?if(number==1){? ? ? ??
? ?System.out.println("可供選擇的車型有:");? ? ? ??
? ?System.out.println("序號\t汽車名稱\t租金\t\t容量");? ? ? ?
? ? for(Car cr:car){? ? ? ? ??
? ? cr.show();? ? ?//遍歷所有的車情況? ? ? ?
? ? }? ? ? ? ? ??
? ? System.out.pr
intln("請你輸入要租汽車的數(shù)量:");? ? ? ?
? ? int num=in.nextInt();? ? ? ??
? ? int flag;??
? ? int ID[]=new int[num];? //用于存儲車的序號? ? ??
? ? for(int i=0;i<num;i++){? ? ? ??
? ? System.out.println("請輸入第"+(i+1)+"個車輛的序號:");? ? ? ? ? ??
? ? flag=in.nextInt();? ? ? ? ?
? ? if(flag>6||flag<0){? ? ? ? ? ? ? ? ? ? ? ?
? ? ? //防止出入超出車序號的車? ? ? ? ? ? ??
? ? System.out.println("你輸入的車序號有誤!");? ? ? ? ? ? ? ? ? ? ? ??
? ? System.out.println("請輸入第"+(i+1)+"個車輛的序號:");? ? ? ? ? ??
? ? flag=in.nextInt();? ? ? ? ?
? ? }? ? ? ? ? ? ? ? ? ? ?
? ? sumprice=sumprice+car[flag-1].price;? ? ? //選了多少輛計算每輛車和的價錢? ?
? ? ID[i]=flag;? ? ? ?//獲取要的車序號? ??
? ? }? ? ? ??
? ? System.out.println("請你輸入要租汽車的天數(shù):");? ? ? ??
? ? int ff=in.nextInt();? ? ? ??
? ? System.out.println("統(tǒng)計完成正在生成你的賬單\n你的賬單如下:\n***可載人的車有***");? ? ? ??
? ? for(int i=0;i<num;i++){? ? ? ??
? ? if(car[ID[i]-1].busload!=0){? ? ? ? ? ? ? ?
? ? sumbusload=sumbusload+car[ID[i]-1].busload;? ? ? //計算能載多少人? ? ? ? ? ? ? ?
? ? System.out.print("\t"+car[ID[i]-1].name);? ? ? ??
? ? ? ? }? ? ? ??
? ? ? ? }? ? ? ??
? ? System.out.println("\t\t共載"+sumbusload+"人");? ? ? ?
? ? System.out.println("***可載貨的車有***");? ? ? ?
? ? ?for(int i=0;i<num;i++){? ? ? ? ??
? ? ?if(car[ID[i]-1].weigth!=0){? ? ? ?//判斷只要是載貨不為0就可以載貨? ? ? ? ? ? ? ?
? ? ?sumweigth=sumweigth+car[ID[i]-1].weigth;? ? ? ? //計算能載多少貨? ? ? ? ? ??
? ? ?System.out.print("\t"+car[ID[i]-1].name);? ? ? ??
? ? ?}? ? ? ?
? ? ? }? ? ? ?
? ? ? ?System.out.println("\t\t共載"+sumweigth+"噸");? ? ? ? ? ? ? ??
? ? ? ?System.out.println("***租出車總價格為:"+sumprice*ff);? ? ? ?
? ? ? ?}? ? ? ??
? ? ? ?else{? ? ? ??
? ? ? ?System.out.println("退出系統(tǒng)!希望你下次能用租車系統(tǒng)!");? ? ? ?
? ? ? ? }??
? ? ? ? }??
? ? ? ? ? ?}
2019-07-20
2019-07-20
請問這里面的無參構(gòu)造方法的作用是什么?
2019-07-19
private被繼承到子類后是無效的,子類可以進行修改使用
2019-07-18
car類里面的屬性用private會比較好(java封裝的特性)