抽象类,接口,用父类对象数组存储用户选择。
过程中最大的难题是怎么将子类对象属性在父类对象数组元素里调用。
问题其实出在子类定义时重写(声明同名变量)了父类中的属性,而根本不用重写,父类中定义了所有子类会用到的属性,这些属性以及方法就会自动继承。
租车系统
package com.dada;
import java.util.Scanner;
public class DadaSystem {
@SuppressWarnings("resource")
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
// car definition
Car car1 = new Car();
car1.name = "奥迪A4"; car1.personLoad = 4; car1.rent = 500;
Car car2 = new Car();
car2.name = "马自达6"; car2.personLoad = 4; car2.rent = 400;
Car car3 = new Car();
car3.name = "金龙"; car3.personLoad = 20; car3.rent = 800;
// truck definition
Truck truck1 = new Truck();
truck1.name = "松花江"; truck1.cargoLoad = 4; truck1.rent = 400;
Truck truck2 = new Truck();
truck2.name = "依维柯"; truck2.cargoLoad = 20; truck2.rent = 1000;
// Pickup definition
Pickup pickup1 = new Pickup();
pickup1.name = "皮卡雪6"; pickup1.cargoLoad = 2; pickup1.personLoad = 4; pickup1.rent = 450;
System.out.println("Welcome to DADA vehicle-renting System! \n Are you gonna rent vehicle? YES-1 NO-0");
int confirm = input.nextInt();
if (confirm == 1){
System.out.println("您可租用的汽车类型及价目表:\n序号\t汽车名称\t租金\t容量");
System.out.println("1.\t"); car1.printInfo();
System.out.println("2.\t"); car2.printInfo();
System.out.println("3.\t"); car3.printInfo();
System.out.println("4.\t"); pickup1.printInfo();
System.out.println("5.\t"); truck1.printInfo();
System.out.println("6.\t"); truck2.printInfo();
}
else {
System.exit(0);
}
System.out.println("请输入您要租用汽车的数量:");
int totalNum = input.nextInt();
int i = 1 ;
Vehicle queue[] = new Vehicle[6];
//利用对象数组保存用户输入的对象
while( i <= totalNum){
System.out.println("请输入第" + i +"辆车的序号:");
switch (input.nextInt()){
case 1:{
queue[i-1] = car1;
break;
}
case 2:{
queue[i-1] = car2;
break;
}
case 3:{
queue[i-1] = car3;
break;
}
case 4:{
queue[i-1] = truck1;
break;
}
case 5:{
queue[i-1] = truck2;
break;
}
case 6:{
queue[i-1] = pickup1;
break;
}
default :{
System.out.println("输入有误!");
break;
}
}
i++;
}
//租车天数
System.out.println("请输入租车天数:");
int rentingdays = input.nextInt();
//利用接口判断并计算载人载货量
int j = 1;
int totalpersonLoad = 0;
int totalcargoLoad = 0;
String[] PersonloadName = new String[4];
String[] CargoloadName = new String[3];
int i1 = 0; int i2 = 0;
//判断载人并用数组保存车型名和总载人数
for(; j <= totalNum ; j++){
if (queue[j-1] instanceof IPersonLoad){
totalpersonLoad += queue[j-1].personLoad;
PersonloadName[i1] = queue[j-1].name;
i1++;
}
else{
continue;
}
}
//判断载货数并用数组保存车型名和总载货数
int k = 1;
for( ; k <= totalNum; k++){
if(queue[k-1] instanceof ICargoLoad){
totalcargoLoad += queue[k-1].cargoLoad;
CargoloadName[i2] = queue[k-1].name;
i2++;
}
else{
continue;
}
}
//输出
System.out.println("您的账单:");
//载人
System.out.println("***可载人的车有:");
int i3 = 0;
do{
System.out.println(PersonloadName[i3] + '\t');
i3++;
} while ((PersonloadName[i3]!=null)&&(i3 <= totalNum));
System.out.println("共载人:" + totalpersonLoad +"人");
//载货
System.out.println("***可载货的车有:");
int i4 = 0;
do{
System.out.println(CargoloadName[i4] + '\t');
i4++;
} while((CargoloadName[i4]!=null)&&(i4 <= totalNum));
System.out.println("共载货:" + totalcargoLoad +"吨");
//总费用
int Singledayrent = 0;
for(i = 0; i <= totalNum-1; i++ ){
Singledayrent += queue[i].rent;
}
float totalrent = Singledayrent * rentingdays;
System.out.println("***租车总价格:" + totalrent + "元");
}
}
抽象父类Vehicle
package com.dada;
public abstract class Vehicle {
public int personLoad;
public int cargoLoad;
public int rent;
public String name;
public abstract void printInfo();
}
子类Car
package com.dada;
public class Car extends Vehicle implements IPersonLoad{
public void printInfo() {
// TODO Auto-generated method stub
System.out.println( name + '\t' +rent + "元/天" +'\t'+ "载人:" + personLoad + "人");
}
}
子类Pickup
package com.dada;
public class Pickup extends Vehicle implements IPersonLoad, ICargoLoad{
@Override
public void printInfo() {
// TODO Auto-generated method stub
System.out.println(name+'\t'+ rent+"元/天" + '\t' + "载货:"+ cargoLoad +"吨" + "载人:"+ personLoad +"人" );
}
}
子类Truck
package com.dada;
public class Truck extends Vehicle implements ICargoLoad{
public void printInfo(){
System.out.println(name+'\t'+ rent+"元/天" + '\t' + "载货:"+ cargoLoad +"吨" );
}
}
载货功能接口(识别作用)
package com.dada;
public interface ICargoLoad {
}
载人功能接口(识别作用)
package com.dada;
public interface IPersonLoad {
}
2025 - 快车库 - 我的知识库 重庆启连科技有限公司 渝ICP备16002641号-10
企客连连 表单助手 企服开发 榜单123