测试类(main)
package com.imooc.rentcar;
import java.util.Scanner;
public class test01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int number;
Car[] car =new Car[7];
Car[] rentcar;
System.out.println("欢迎使用哒哒租车系统:您是否要租车? 1是0否");
System.out.println();
if(in.nextInt()==1){
System.out.println("您可租车的类型及价目表:\n"+
"序号\t汽车名称\t租金\t容量");
car[1]=new LoadPersonCar("奥迪A4",500,4);
car[2]=new LoadPersonCar("马自达6",400,4);
car[3]=new PickUp("皮卡雪6",450,4,2);
car[4]=new LoadPersonCar("金龙",800,20);
car[5]=new LoadThingCar("松花江",400,4);
car[6]=new LoadThingCar("依维柯",1000,20);
for(int i=1;i<=car.length-1;i++){
if(car[i].PersonNum==0)
System.out.println(i+"\t"+car[i].name+"\t"+car[i].price+"\t"+"载物"+car[i].ThingNum);
else if(car[i].ThingNum==0)
System.out.println(i+"\t"+car[i].name+"\t"+car[i].price+"\t"+"载人"+car[i].PersonNum);
else
System.out.println(i+"\t"+car[i].name+"\t"+car[i].price+"\t"+"载人"+car[i].PersonNum+"载物"+car[i].ThingNum);
}
System.out.println("请输入您要租车数量:");
number=in.nextInt();
rentcar=new Car[number];
for(int i=1;i<=number;i++){
System.out.println("请输入第"+i+"辆的序号");
int a =in.nextInt();
rentcar[i-1]=car[a];
}
System.out.println("请输入租车天数");
int b=in.nextInt();
System.out.println();
System.out.println("您的账单:");
int TotalPeopleSum=0;
int TotaThingSum=0;
int TotalPrice=0;
for(Car mycar:rentcar){
System.out.println(mycar.name);
TotalPeopleSum+=mycar.PersonNum;
TotaThingSum+=mycar.ThingNum;
TotalPrice+=mycar.price;
}
System.out.println("总载客:"+TotalPeopleSum+",总载物"+TotaThingSum+",总价格"+TotalPrice*b);
}
}
}
车的父类
package com.imooc.rentcar;
public class Car {
String name;
int price;
int PersonNum;
int ThingNum;
public Car(String name,int price){
this.name=name;
this.price=price;
}
}
载人车
package com.imooc.rentcar;
public class LoadPersonCar extends Car {
public LoadPersonCar(String name, int price) {
super(name, price);
// TODO Auto-generated constructor stub
}
public LoadPersonCar(String name,int price,int PersonNum) {
// TODO Auto-generated constructor stub
this(name, price);
this.PersonNum=PersonNum;
}
}
载物车
package com.imooc.rentcar;
public class LoadThingCar extends Car {
public LoadThingCar(String name, int price) {
super(name, price);
// TODO Auto-generated constructor stub
}
public LoadThingCar(String name,int price,int ThingNum) {
// TODO Auto-generated constructor stub
this(name, price);
this.ThingNum=ThingNum;
}
}
皮卡
package com.imooc.rentcar;
public class PickUp extends Car {
public PickUp(String name, int price) {
super(name, price);
// TODO Auto-generated constructor stub
}
public PickUp(String name,int price,int PersonNum,int ThingNum) {
// TODO Auto-generated constructor stub
this(name, price);
this.PersonNum=PersonNum;
this.ThingNum=ThingNum;
}
}
运行结果
热门评论
Car[] car =new Car[7];
这个什么意思?
为什么不直接
public LoadThinigCar(String name,int price,int ThingNum){
super(name,price);
this.ThingNum = ThingNum;
}
呢...感觉没差啊
你好 我想问问为什么子类里要创建两次