首页 > IT职业 > IT图书 > 借鉴了其他同学的写了半天呢 还是不熟悉
父类
package car;
public abstract class Car{
protected int price;//车的租金
protected String name;//车的名字
protected int passengerLoad;//载人数量
protected double goodsLoad;//载货重量
public int getPrice(){
return price;
}
public void setPrice(int price){//执行方法
this.price=price;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getPassengerLoad(){
return passengerLoad;
}
public void setpassengerLoad(int passengerLoad){
this.passengerLoad=passengerLoad;
}
public double getGoodsLoad(){
return goodsLoad;
}
public void setgoodsLoad(double goodsLoad){
this.goodsLoad=goodsLoad;
} }
客车
package car;
public class keCar extends Car {
public keCar (String name,int price,double goodsLoad,int passengerLoad){
this.setName(name);
this.setPrice(price);
this.setpassengerLoad(passengerLoad);
} public String toString(){
return this.getName()+"----"+this.getPrice()+"元/天----载客"+this.getPassengerLoad()+"人";
}
}
货车
public class truck extends Car{
public truck(String name,int price,double goodsLoad,int passengerLoad){
this.setName(name);
this.setPrice(price);
this.setgoodsLoad(goodsLoad);
} public String toString(){
return this.getName()+"----"+this.getPrice()+"元/天----载客"+this.getGoodsLoad()+"货";
}
}
两用
public class bothCarry extends Car{
public bothCarry (String name,int price,double goodsLoad,int passengerLoad){
this.setName(name);
this.setPrice(price);
this.setpassengerLoad(passengerLoad);
this.setgoodsLoad(goodsLoad);
}
public String toString(){
return this.getName()+"----"+this.getPrice()+"元/天----载客"+this.getPassengerLoad()+"人"+this.getGoodsLoad()+"货";
}
}
test
public class text {
public static void main(String[] args) {
// TODO Auto-generated method stub Scanner sc=new Scanner(System.in);
System.out.println("欢迎使用答答租车系统");
System.out.println();
System.out.println("租车请按(1) 推出请按(0)");
int num=sc.nextInt();
Car[] cars={new keCar("奥迪A4",300,0,4),
new keCar("雪佛兰5",250,0,4),
new keCar("宝骏巴士",800,0,45),
new truck("东风R1",1000,10,0),
new truck("依维柯S",1500,25,0),
new bothCarry("皮卡雪Z",600,5,2)};
if(num!=1){
System.out.println("欢迎下次光临**");
System.exit(0);
}else{
System.out.println("请输入你要预订的数量") ;
}
int priceSum=0;
double goodsLoadSum=0;
int passengerLoadSum=0;
int i=sc.nextInt();
for(int j=1;j System.out.println("谢谢惠顾");
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4604/viewspace-2798483/ ,如需转载,请注明出处,否则将追究法律责任。