package com.imooc.practice61; import java.util.Scanner; //测试类 public class First { public static final Car[] car={ new PassengerCar(1,"奥迪A4",500,4), new PassengerCar(2,"马自达6",400,4), new PickupTruck(3,"皮卡雪6",450,4,2), new PassengerCar(4,"金龙",450,20), new Truck(5,"松花江",450,4), new Truck(6,"依维柯",450,40) };//数组定义车基本信息 public static void main(String[] args) { // TODO Auto-generated method stub Scanner a=new Scanner(System.in); int sum=0; String smanned=""; String sladenCargo=""; System.out.println("欢迎使用答答租车系统:"+"\n"+"您是否要租车:1是 0否"); int b=a.nextInt(); if (b==0){ System.out.println("已退出程序,再见!!!"); return; } if(b==1){ System.out.println("您可租车的类型及其价目表:"); System.out.println("序号 汽车名称 租金 容量"); } while(b!=0 && b!=1){ System.out.println("请输入有效的选项!!您是否要租车:1是 0否"); int c=a.nextInt(); } for (int k=0;k<car.length;k++){ System.out.println(car[k]); } System.out.println("请输入您想要的租车数量:"); int d=a.nextInt(); int[] Array=new int[d];//定义一个新的数组,用于存放键盘输入的租车信息 int l=1; for (int i=0;i<d;i++){//循环保存用户想要的租车序号存入数组 System.out.println("请输入第"+ l +"辆车的序号:"); int e=a.nextInt(); if(e<=car.length){ e=e-1; Array[i]=e; l++; }else{ System.out.println("您输入的序号有误,请重新输入!"); i--; } } System.out.println("请输入租车天数:"); int f=a.nextInt(); System.out.println("您的账单:"); for(int j=0;j<Array.length;j++){ sum +=car[Array[j]].getPrice()*f;//循环计算车的总价格 if(!(car[Array[j]].getManned()==0)){ smanned = smanned+String.valueOf(car[Array[j]].getCarName()+" ");} if(!(car[Array[j]].getLadenCargo()==0)){ sladenCargo = sladenCargo+String.valueOf(car[Array[j]].getCarName()+" "); } } System.out.println("***可载人的车有:"+ smanned); System.out.println("***可载货的车有:"+ sladenCargo); System.out.println("***租车总价格:"+sum); } } //Car父类 package com.imooc.practice61; public class Car { private int no;//序号 private String carName;//车名 private double price;//价格 private int manned;//载人 private int ladenCargo;//载货 public int getManned() { return manned; } public void setManned(int manned) { this.manned = manned; } public int getLadenCargo() { return ladenCargo; } public void setLadenCargo(int ladenCargo) { this.ladenCargo = ladenCargo; } public Car(){ } public Car(int no,String carName,double price,int manned,int ladenCargo){ this.no=no; this.carName=carName; this.price=price; this.manned=manned; this.ladenCargo=ladenCargo; } public int getNo() { return no; } public void setNo(int no) { this.no = no; } public String getCarName() { return carName; } public void setCarName(String carName) { this.carName = carName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } } //载人车子类 package com.imooc.practice61; public class PassengerCar extends Car { public PassengerCar(int no,String carName,double price,int manned){ super.setNo(no); super.setCarName(carName); super.setPrice(price); super.setManned(manned); } public String toString(){ return super.getNo()+". "+ super.getCarName()+" "+ super.getPrice()+"元/天 "+ "载人:"+ super.getManned()+"人"; } } //卡皮车子类,既能载货也能载人 package com.imooc.practice61; public class PickupTruck extends Car { public PickupTruck(int no,String carName,double price,int manned,int ladenCargo){ super.setNo(no); super.setCarName(carName); super.setPrice(price); super.setManned(manned); super.setLadenCargo(ladenCargo); } public String toString(){ return super.getNo()+". "+ super.getCarName()+" " + super.getPrice()+"元/天 "+"载人:"+ super.getManned()+"人"+ "载货:"+ super.getLadenCargo()+"吨"; } } //载货车子类 package com.imooc.practice61; public class Truck extends Car { public Truck (int no,String carName,double price,int ladenCargo){ super.setNo(no);//调用父类的方法 super.setCarName(carName); super.setPrice(price); super.setLadenCargo(ladenCargo); } public String toString(){ return super.getNo()+". "+ super.getCarName()+" " + super.getPrice()+"元/天 "+"载货:"+ super.getLadenCargo()+"吨"; } }
小测试一枚,学习JAVA???
热门评论
请问父类中
这个无参构造方法是用来干什么的呢?在下刚学 不是很清楚