抽象类car
import java.util.*;
public abstract class Car {
public String name;
public int num;
public int person;
public int loads;
public int rent;
public void setNum(int num) {
this.num=num;
}
public int getNum() {
return num;
}
public void setRent(int rent) {
this.rent=rent;
}
public int getRent() {
return rent;
}
public void setPerson(int person) {
this.person=person;
}
public int getPerson() {
return person;
}
public void setLoads(int loads) {
this.loads=loads;
}
public int getLoads() {
return loads;
}
public void setName(String name) {
this.name=name;
}
public String getName() {
return name;
}
}
子类轿车Auto
public class Auto extends Car {
public Auto(int num,String name,int person,int rent) {
this.setNum(num);
this.setName(name);
this.setPerson(person);
this.setRent(rent);
}
public String toString() {
return (num+"\t"+name+"\t"+"载人"+person+"位"+"\t\t"+rent);
}
}
子类卡车Truck
public class Truck extends Car {
public Truck(int num,String name,int loads,int rent) {
this.setNum(num);
this.setName(name);
this.setLoads(loads);;
this.setRent(rent);
}
public String toString() {
return (num+"\t"+name+"\t"+"载货"+loads+"吨"+"\t\t"+rent);
}
}
子类皮卡Van
public class Van extends Car {
public Van(int num,String name,int person,int loads,int rent) {
this.setNum(num);
this.setName(name);
this.setPerson(person);
this.setLoads(loads);
this.setRent(rent);
}
public String toString() {
return (num+"\t"+name+"\t"+"载人"+person+"位,载货"+loads+"吨\t"+rent);
}
}
主程序
import java.util.Scanner;
public class Carsys {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int rentNum=0;//租车数量
int rentDay=0;//租车天数
//初始化车辆列表
Car cars[]=new Car[6];
cars[0]= new Auto(1,"奥迪A4",4,500);
cars[1]= new Auto(2,"马自达6",4,400);
cars[2]= new Van(3,"皮卡雪6",4,2,450);
cars[3]= new Auto(4,"金龙",20,800);
cars[4]= new Truck(5,"松花江",4,400);
cars[5]= new Truck(6,"依维柯",20,1000);
cars[0].getName();
System.out.println("您好!欢迎使用哒哒租车系统");
while(true) {
System.out.println("请选择:租车服务输入1,退出系统输入2");
int a=input.nextInt();
if(a==2) {
System.out.println("谢谢使用,再见!");
break;
}else if(a==1){
System.out.println("以下是租车列表:");
System.out.println("编 号"+"\t类 型"+"\t容 量"+"\t\t日租金/天");
for(Car each:cars) {
System.out.println(each);
}
System.out.println("请输入您要租车的数量");
rentNum=input.nextInt();
Car rentCar[]=new Car[rentNum];
int price=0;
int totalPerson=0;
int totalLoads=0;
for(int i=0;i<rentNum;i++) {
while(true) {
System.out.println("请输入第"+(i+1)+"辆车的编号:");
int b=input.nextInt();
if(b>6||b<0) {
System.out.println("输入有误");
}else {
rentCar[i]=cars[b-1];
break;
}
}
}
for(int i=0;i<rentNum;i++) {
totalPerson=totalPerson+rentCar[i].person;
}
for(int i=0;i<rentNum;i++) {
totalLoads=totalLoads+rentCar[i].loads;
}
System.out.println("请输入租车天数");
rentDay=input.nextInt();
for(int i=0;i<rentNum;i++) {
price=price+rentCar[i].rent*rentDay;
}
System.out.println("您的账单如下:");
System.out.println("***可载人的车有:");
for(int i=0;i<rentNum;i++) {
if(rentCar[i].person!=0)
System.out.println(rentCar[i].name);
}
System.out.println("共可载人"+totalPerson+"人");
System.out.println("***可载货的车有:");
for(int i=0;i<rentNum;i++) {
if(rentCar[i].loads!=0)
System.out.println(rentCar[i].name);
}
System.out.println("共可载货"+totalLoads+"吨");
System.out.println("***租车总价格:"+price+"元");
System.out.println("感谢惠顾,欢迎下次光临!");
break;
}else {
System.out.println("输入有误,请重新输入");
}
}
input.close();
}
}
cars[0].getName();这个用意是什么?我是java小白一枚
父类为什么要用抽象类?
非常好
我是觉得你属性声明都是public,就直接赋值好了。要么就声明为private,使用get set。
66666
很完美了
基础很重要