long_l
2017-07-17 21:08
父类
package com.zuche;
public class CarProperty {
/*public String carP1;//车辆名称
public String carP2; //车辆类型
public int carP3;//载客量
public int carP4;//载货量
public int carP5; //租金每天
*/
public void show(String carP1,String carP2,int carP3,int carP4,int carP5) {
System.out.println(" "+carP1+" "+carP2+" "+carP3+" "
+ " "+carP4+" "+carP5);
}
}子类
package com.zuche;
public class HuoChe extends CarProperty{
}package com.zuche;
public class KeChe extends CarProperty {
}package com.zuche;
public class PiKa extends CarProperty {
}测试
package com.zuche;
import java.util.*;
public class Initial {
public static void main(String[] args) {
// TODO Auto-generated method stub
CarProperty huo1=new HuoChe();
CarProperty huo2=new HuoChe();
CarProperty ke1=new KeChe();
CarProperty ke2=new KeChe();
CarProperty pi1=new PiKa();
CarProperty pi2=new PiKa();
int sum=0;
int i3=0;
System.out.println("请您选择是否租车?");
System.out.println("1,是 2,否");
Scanner input1=new Scanner(System.in);
int i1=input1.nextInt();
if(i1==1) {
System.out.println("车辆名称 车辆类型 载客量/人 载货量/吨 租金/每天");
huo1.show("1,奥迪", "货车", 2, 3, 100);
huo2.show("2,奥拓", "货车", 2, 2, 80);
ke1.show("3,宝马", "客车", 4, 0, 150);
ke2.show("4,一汽", "客车", 4, 0, 100);
pi1.show("5,五菱", "两用", 8, 2, 80);
pi2.show("6,奔马", "两用", 2, 3, 60);
System.out.println("请您选择租车数量:");
Scanner input2=new Scanner(System.in);
int i2=input2.nextInt();
for(int i=1;i<i2+1;i++) {
System.out.println("请输入您要租用的第"+i+"辆车的编号");
Scanner input3=new Scanner(System.in);
i3=input3.nextInt();
if(i3<0||i3>6) {
System.out.println("请检查您输入的编号是否正确!");
break;
}else {
if(i3==1) {
sum=sum+100;
}
if(i3==2) {
sum=sum+80;
}
if(i3==3) {
sum=sum+150;
}if(i3==4) {
sum=sum+100;
}
if(i3==5) {
sum=sum+80;
}if(i3==6) {
sum=sum+60;
}}
}if(i3>0&&i3<7) {
System.out.println("请输入租用的天数");
Scanner input4=new Scanner(System.in);
int sum1=input4.nextInt();
sum=sum*sum1;
System.out.println("总共租车:"+i2+"辆,租用:"+sum1+"天,共需:"+sum+"元人民币。");
}else {
System.out.println("请重新操作!");
} }else {
System.out.println("感谢使用该系统!");
}
}
}我自己感觉很简单粗暴。
这个项目代码量不大,父类可以写成虚拟类,子类重写父类输出函数,就可以省去在主函数里分开计算的诸多else if语句
写的真好,但是如果有总载人,总载货,以及输出载人的有,载货的有,该怎么往这个里面加呢??
感觉你写的非常不错,我写的代码没有用到继承和抽象类非常繁琐。
Java入门第二季
531292 学习 · 6327 问题
相似问题