时代花园
2018-09-13 16:17
*********父类*************
package com.zonghelianxi1;
public abstract class Car {
public int id;
public String name;
public int price;
public int sit;
public int load;
/*public int getSit() { //getSit方法也可可以不要,直接在测试类中调用sit值即可
return sit;
}
*/
//public Car(){}; //这段没用
/*public Car(int id,String name,int price,int sit,int load){
this.id=id;
this.name=name;
this.price=price;
this.sit=sit;
this.load=load;
}
*/
public abstract void show();
}
************子类**************
package com.zonghelianxi1;
public class Seden extends Car {
public Seden(int id,String name,int price,int sit,int load){
this.id=id;
this.name=name;
this.price=price;
this.sit=sit;
this.load=load;
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println(id+".\t"+name+"\t"+price+"元/天 "+"\t"+"载人:"+sit+"\t"+"载货:"+load+"吨");
}
}
*************子类****************
package com.zonghelianxi1;
public class Pickup extends Car {
public Pickup(int id,String name,int price,int sit,int load){
this.id=id;
this.name=name;
this.price=price;
this.sit=sit;
this.load=load;
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println(id+".\t"+name+"\t"+price+"元/天 "+"\t"+"载人:"+sit+"\t"+"载货:"+load+"吨");
}
}
**********子类**********
package com.zonghelianxi1;
public class Truck extends Car {
public Truck(int id,String name,int price,int sit,int load){
this.id=id;
this.name=name;
this.price=price;
this.sit=sit;
this.load=load;
}
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println(id+".\t"+name+"\t"+price+"元/天 "+"\t"+"载人:"+sit+"\t"+"载货:"+load+"吨");
}
}
************测试类*************
package com.zonghelianxi1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car a=new Seden(1,"奥迪A4",500,4,0);
Car b=new Seden(2,"马自达6",400,4,0);
Car c=new Pickup(3,"皮卡雪6",450,4,2);
Car d=new Seden(4,"金龙",800,20,0);
Car e=new Truck(5,"松花江",400,0,4);
Car f=new Truck(6,"依维柯",1000,0,20);
Car[] car= {a,b,c,d,e,f}; //定义数组
Scanner in=new Scanner(System.in);
System.out.println("欢迎使用答答租车系统");
System.out.println("你是否要租车:\t1.是\t2.否");
int shifouzuche=in.nextInt();
while(shifouzuche!=1) {
Scanner in1=new Scanner(System.in);
System.out.println("欢迎使用答答租车系统");
System.out.println("你是否要租车:\t1.是\t2.否");
shifouzuche=in1.nextInt();
}
System.out.println(" "); //这一句并没什么用,只是为了留出间隔、看起来好看点,以下也一样
if(shifouzuche==1) {
System.out.println("您可租车的类型及其价目表");
System.out.println("序号\t汽车名称\t租金\t容量(人)\t容量(货)");
}
a.show();
b.show();
c.show();
d.show();
e.show();
f.show();
System.out.println(" ");
Scanner in2=new Scanner(System.in);
System.out.println("请输入您要租汽车的数量:");
int zucheshuliang=in2.nextInt();
while(zucheshuliang<=0||zucheshuliang>6) {
Scanner in3=new Scanner(System.in);
System.out.println("请输入您要租汽车的数量:");
zucheshuliang=in3.nextInt();
}
System.out.println(" ");
Scanner in4=new Scanner(System.in);
System.out.println("请输入租的天数:");
int time=in4.nextInt();
int sumprice=0;
int sumsit=0;
int sumload=0;
for(int i=1;i<=zucheshuliang; i++) {
System.out.println(" ");
System.out.println("请输入第"+i+"辆车的序号:");
Scanner in5=new Scanner(System.in);
int xuHao=in5.nextInt();
sumprice=sumprice+car[xuHao-1].price; //不必调用getPrice方法
if(car[xuHao-1].sit!=0) {
sumsit=sumsit+car[xuHao-1].sit;
System.out.print(car[xuHao-1].name+"可载人 "+" ");
}
if(car[xuHao-1].load!=0) {
sumload=sumload+car[xuHao-1].load;
System.out.print(car[xuHao-1].name+"可载货 "+" ");
}
System.out.println(" ");
}
System.out.println(" ");
System.out.println("您的账单: ");
System.out.println("总载客量:"+sumsit);
System.out.println("总载货量:"+sumload+"吨");
System.out.println("总价格:"+sumprice*time+"元");
}
}
很棒!想要两积分,谢谢采纳
wdnmd点赞!
测试类的有些代码看不懂
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题