亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

感覺還是有很多不足的地方希望大神可以幫我看看

package Ns;


public abstract class Cars {

? public int id;? ? ? ? ? ?//定義車輛序號??

? public String name;? ? ? //車輛的名稱? ?

? public double price;? ? ?//定義價格? ?

? public int busload;? ? ? //載客量? ?

? public int weigth;? ? ? ?//載重

? public int type;? ? ? ?// 0為載人類型的車 1為載貨的車

??

? public Cars(){

? //無參的構造方法

? }

? public Cars(int id,String name,double price,int busload,int weigth,int type){ //有參的構造方法

? this.id=id;

? this.name=name;

? this.price=price;

? this.busload=busload;

? this.weigth=weigth;

? this.type=type;

? }

? public abstract void show();? //定義抽象方法

// ? 如果一個類包含抽象方法,那么該類必須是抽象類。

// ? 任何子類必須重寫父類的抽象方法,或者聲明自身為抽象類。

// ? 繼承抽象方法的子類必須重寫該方法。否則,該子類也必須聲明為抽象類。最終,必須有子類實現該抽象方法,否則,從最初的父類到最終的子類都不能用來實例化對象。

}

//定義汽車類

//在同一個類中只需要一個public類,切必須于類名相同

class bus extends Cars{

? public bus(int id,String name,double price,int busload,int type){

? this.id=id;? ? ??

? this.name=name;? ??

? this.price=price;? ? ??

? this.busload=busload;

? this.type=type;

? }?

? public void show(){? ? ??

? System.out.println(id+"\t"+name+"\t"+price+"元/天? "+"\t載人:"+busload+"人");?

? }

};

//定義即可載人也可以載貨

class Pick extends Cars{?

public Pick(){? ? ? ? ?}??

public Pick(int id,String name,double price,int busload,int weigth,int type){? ? ?

super(id,name,price,busload,weigth,type);? //直接使用父類的變量

// ? this和super差異

// ? ?1)super(參數):調用基類中的某一個構造函數(應該為構造函數中的第一條語句)?

//   2)this(參數):調用本類中另一種形成的構造函數(應該為構造函數中的第一條語句)

//   3)super: 它引用當前對象的直接父類中的成員(用來訪問直接父類中被隱藏的父類中成員數據或函數,基類與派生類中有相同成員定義時如:super.變量名? ? super.成員函數據名(實參)

//   4)this:它代表當前對象名(在程序中易產生二義性之處,應使用this來指明當前對象;如果函數的形參與類中的成員數據同名,這時需用this來指明成員變量名)

//   5)調用super()必須寫在子類構造方法的第一行,否則編譯不通過。每個子類構造方法的第一條語句,都是隱含地調用super(),如果父類沒有這種形式的構造函數,那么在編譯的時候就會報錯。

//   6)super()和this()類似,區別是,super()從子類中調用父類的構造方法,this()在同一類內調用其它方法。

//   7)super()和this()均需放在構造方法內第一行。

//   8)盡管可以用this調用一個構造器,但卻不能調用兩個。

//   9)this和super不能同時出現在一個構造函數里面,因為this必然會調用其它的構造函數,其它的構造函數必然也會有super語句的存在,所以在同一個構造函數里面有相同的語句,就失去了語句的意義,編譯器也不會通過。

//   10)this()和super()都指的是對象,所以,均不可以在static環境中使用。包括:static變量,static方法,static語句塊。

//   11)從本質上講,this是一個指向本對象的指針, 然而super是一個Java關鍵字。

}

public void show(){? ? ??

? System.out.println(id+"\t"+name+"\t"+price+"元/天? "+"\t載人"+busload+"\t載貨:"+weigth+"噸");?

? ?}

}??

class name extends Cars{

public void show() {

System.out.println("序號"+"\t名稱"+"\t價格"+"\t\t載人"+"\t載重");

}

}




package Ns;


import java.util.Scanner;



public class Texts {


public static void main(String[] args) {

// TODO Auto-generated method stub

Cars[] an={

new name(),

new bus(1, "奧迪", 200, 4,0),

new bus(2, "公交車", 600, 30,0),

new Pick(3, "拖拉機", 300, 2, 5,1),

new Pick(4, "卡車", 300, 4, 20,1),

};

Scanner in=new Scanner(System.in);

int ans=0;;

double sumprice=0;??

int asn=0;

int zhuo=0;?

int count=0;

String names="";

String namesn="";

System.out.println("您是否要用噠噠租車系統 1是 0否");

int a=in.nextInt();

if(a==1){

System.out.println("您可租車的類型及價格表");

for(Cars cr:an){? //相當于C# foreach,使用foreach可以迭代數組或者一個集合對象。

cr.show();? //遍歷所有的車情況?

}?

System.out.println("請您選擇要租車的數量");

int b=in.nextInt();

int ID[]=new int[b]; //用于存儲車輛的數量

for (int i = 1; i <= ID.length; i++) {

System.out.println("請您輸入第"+i+"輛車序號");

ans=in.nextInt();

if (an[ans].type==0) {

asn=asn+an[ans].busload;? ?//計算載人數

sumprice=sumprice+an[ans].price;? //計算載人租車價格

names =names+ an[ans].name+",";? //獲取名稱

}

if(an[ans].type==1){

zhuo=zhuo+an[ans].weigth;? ?//計算載貨數

sumprice=sumprice+an[ans].price;? //計算載人租車價格

namesn =namesn+ an[ans].name+",";

}

}

System.out.println("請輸入租車的天數");

count=in.nextInt();

System.out.println("您的賬單詳情如下.........");

System.out.println("*******可以載人的車有*******");

System.out.println(names+"共載"+asn+"人");

System.out.println("*******可以載貨的車有*******");

System.out.println(namesn+"共載"+zhuo+"噸");

System.out.println("共計消費"+sumprice*count+"元");

in.close();

}

}

}


正在回答

1 回答

哇塞,真的不錯唉。但是我不知道怎么做,沒有一點思路。


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

感覺還是有很多不足的地方希望大神可以幫我看看

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號