- 父类
public class Car {
//int people;
String name;
int money;
public Car(String name1,int money1){
name=name1;
money=money1;
}
} - 子类1(只载人)
public class PassengerCar extends Car {
int people;
public PassengerCar(String name1, int money1,int people1) {
super(name1, money1);
people=people1;
System.out.println("1\t"+name+"\t"+money+"\t"+"载人"+people+"人\t" );
}
} - 子类2(只载物)
public class Van extends Car {
// String name;
int weight;
public Van(String name1,int money1,int weight1){
super(name1, money1);
name=name1;
money=money1;
weight=weight1;
System.out.println("6\t"+name+"\t"+money+"\t"+"载货"+weight+"吨\t" );
}
} - 子类3(载人又载物)
public class Truck extends Car {
int people;
int weight;
public Truck(String name1, int money1,int people1,int weight1) {
super(name1, money1);
people=people1;
weight=weight1;
System.out.println("3\t"+name+"\t"+money+"\t"+"载人"+people+"人"+"载货"+weight+"吨\t" );
}
} - 测试类
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int sum=0;//金额
String n = "";//记录载人的车子
String w="";//记录载物的车子
int p=0;//计算载人总人数
int h=0;//货物总量
System.out.println("是否选择哒哒租车系统:1是 0否");
Scanner input=new Scanner(System.in);
// int a=input.nextInt();
if(input.nextInt()==1){
System.out.println("序号\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量\t ");
PassengerCar aCar=new PassengerCar("奥迪A4",500,4);
PassengerCar bCar=new PassengerCar("马自达6",500,4);
Truck cCar=new Truck("皮卡雪6",450,2,2);
PassengerCar dCar=new PassengerCar("金龙",800,20);
Van eCar=new Van("松花江",400, 4);
Van fCar=new Van("依维柯",1000,20);
System.out.println("请输入你需要租车的数量");
Scanner intput=new Scanner(System.in);
int b = input.nextInt();//接受你租车的数量
for(int i=1;i<=b;i++){
System.out.println("请输入第"+i+"辆车的序号: ");
int no=input.nextInt();
switch (no) {
case 1:
sum=sum+aCar.money;
n=n+aCar.name+" ";
p=p+aCar.people;
break;
case 2:
sum=sum+bCar.money;
n=n+bCar.name+" ";
p=p+bCar.people;
break;
case 3:
sum=sum+cCar.money;
n=n+cCar.name+" ";
w=w+cCar.name+" ";
p=p+cCar.people;
h=h+cCar.weight;
break;
case 4:
sum=sum+dCar.money;
n=n+dCar.name+" ";
p=p+dCar.people;
break;
case 5:
sum=sum+eCar.money;
w=w+eCar.name+" ";
h=h+eCar.weight;
break;
case 6:
sum=sum+fCar.money;
w=w+fCar.name+" ";
h=h+fCar.weight;
break;
}
}
System.out.println("请输入租车天数: ");
int c=input.nextInt();//接收租车天数
System.out.println("载人的车子有:"+n+" 能载人:"+p+"人");
System.out.println("载物的车子有:"+w+" 能载物:"+h+"吨");
System.out.println("你的租金"+sum*c);
}
else {
System.err.println("你已经退出本系统");
}
}
}
- 结果
热门评论
新手写的这么好啊,弄得我羞愧难当啊! 现在混得咋样了