package ZhuCheXiTong; //定义抽象车的父类 public abstract class Car { public int id; //定义车辆序号 public String name; //车辆的名称 public double price; //定义价格 public int busload; //载客量 public int weigth; //载重量 public int getId() { //封装对象 return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getBusload() { return busload; } public void setBusload(int busload) { this.busload = busload; } public int getWeigth() { return weigth; } public void setWeigth(int weigth) { this.weigth = weigth; } public Car(){ //无参构造 } public Car(int id,String name,double price,int busload,int weigth){ this.id=id; this.name=name; //有参构造 this.price=price; this.busload=busload; this.weigth=weigth; } public abtract void show(); //定义抽象方法} //定义可以载人的车 class Busload extends Car{ public Busload(){ //无参构造 } public Busload(int id,String name,double price,int busload){ // super(); this.id=id; this.name=name; this.price=price; this.busload=busload; } public void show(){ System.out.println(id+"\t"+name+"\t"+price+"元/天 "+"\t载人:"+busload+"人"); } } //定义可以载货的车 class Weigth extends Car{ public Weigth(){ } public Weigth(int id,String name,double price,int weigth){ // super(); this.id=id; this.name=name; this.price=price; this.weigth=weigth; } public void show(){ System.out.println(id+"\t"+name+"\t"+price+"元/天 "+"\t载货:"+weigth+"吨"); } } //定义即可载人也可以载货 class Pick extends Car{ public Pick(){ } public Pick(int id,String name,double price,int busload,int weigth){ super(id,name,price,busload,weigth); //直接使用父类的变量 // this.id=id; // this.name=name; // this.price=price; // this.busload=busload; // this.weigth=weigth; } public void show(){ System.out.println(id+"\t"+name+"\t"+price+"元/天 "+"\t载人:"+busload+"人"+",载货:"+weigth+"吨"); } } package ZhuCheXiTong; import java.util.Scanner; //测试租车系统 public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Car[] car= { new Busload(1,"奥迪A4",500,4), new Busload(2,"马自达",400,4), new Pick(3,"皮卡雪",450,4,2), new Busload(4,"金龙",800,20), new Weigth(5,"松花江",400,4), new Weigth(6,"依维柯",1000,20), }; double sumprice=0; int sumbusload=0; int sumweigth=0; Scanner in=new Scanner(System.in); System.out.println("欢迎使用嗒嗒租车系统:\n你是否要租车:1-是 0-否"); int number=in.nextInt(); if(number==1){ System.out.println("可供选择的车型有:"); System.out.println("序号\t汽车名称\t租金\t\t容量"); for(Car cr:car){ cr.show(); //遍历所有的车情况 } System.out.println("请你输入要租汽车的数量:"); int num=in.nextInt(); int flag; int ID[]=new int[num]; //用于存储车的序号 for(int i=0;i<num;i++){ System.out.println("请输入第"+(i+1)+"个车辆的序号:"); flag=in.nextInt(); if(flag>6||flag<0){ //防止出入超出车序号的车 System.out.println("你输入的车序号有误!"); System.out.println("请输入第"+(i+1)+"个车辆的序号:"); flag=in.nextInt(); } sumprice=sumprice+car[flag-1].price; //选了多少辆计算每辆车和的价钱 ID[i]=flag; //获取要的车序号 } System.out.println("请你输入要租汽车的天数:"); int ff=in.nextInt(); System.out.println("统计完成正在生成你的账单\n你的账单如下:\n***可载人的车有***"); for(int i=0;i<num;i++){ if(car[ID[i]-1].busload!=0){ sumbusload=sumbusload+car[ID[i]-1].busload; //计算能载多少人 System.out.print("\t"+car[ID[i]-1].name); } } System.out.println("\t\t共载"+sumbusload+"人"); System.out.println("***可载货的车有***"); for(int i=0;i<num;i++){ if(car[ID[i]-1].weigth!=0){ //判断只要是载货不为0就可以载货 sumweigth=sumweigth+car[ID[i]-1].weigth; //计算能载多少货 System.out.print("\t"+car[ID[i]-1].name); } } System.out.println("\t\t共载"+sumweigth+"吨"); System.out.println("***租出车总价格为:"+sumprice*ff); } else{ System.out.println("退出系统!希望你下次能用租车系统!"); } } }
package com.imooc;
import com.imooc.Car;
import java.util.Scanner;
public class Home {
//测试租车系统
public static void main(String[] args) {
// TODO Auto-generated method stub
Car[] car=
{
new Busload(1,"奥迪A4",500,4),
new Busload(2,"马自达",400,4),
new Pick(3,"皮卡雪",450,4,2),
new Busload(4,"金龙",800,20),
new Weigth(5,"松花江",400,4),
new Weigth(6,"依维柯",1000,20),
};
double sumprice=0;
int sumbusload=0;
int sumweigth=0;
Scanner in=new Scanner(System.in);
System.out.println("欢迎使用嗒嗒租车系统:\n你是否要租车:1-是 0-否");
int number=in.nextInt();
if(number==1){
System.out.println("可供选择的车型有:");
System.out.println("序号\t汽车名称\t租金\t\t容量");
for(Car cr:car){
cr.show(); //遍历所有的车情况
}
System.out.pr
intln("请你输入要租汽车的数量:");
int num=in.nextInt();
int flag;
int ID[]=new int[num]; //用于存储车的序号
for(int i=0;i<num;i++){
System.out.println("请输入第"+(i+1)+"个车辆的序号:");
flag=in.nextInt();
if(flag>6||flag<0){
//防止出入超出车序号的车
System.out.println("你输入的车序号有误!");
System.out.println("请输入第"+(i+1)+"个车辆的序号:");
flag=in.nextInt();
}
sumprice=sumprice+car[flag-1].price; //选了多少辆计算每辆车和的价钱
ID[i]=flag; //获取要的车序号
}
System.out.println("请你输入要租汽车的天数:");
int ff=in.nextInt();
System.out.println("统计完成正在生成你的账单\n你的账单如下:\n***可载人的车有***");
for(int i=0;i<num;i++){
if(car[ID[i]-1].busload!=0){
sumbusload=sumbusload+car[ID[i]-1].busload; //计算能载多少人
System.out.print("\t"+car[ID[i]-1].name);
}
}
System.out.println("\t\t共载"+sumbusload+"人");
System.out.println("***可载货的车有***");
for(int i=0;i<num;i++){
if(car[ID[i]-1].weigth!=0){ //判断只要是载货不为0就可以载货
sumweigth=sumweigth+car[ID[i]-1].weigth; //计算能载多少货
System.out.print("\t"+car[ID[i]-1].name);
}
}
System.out.println("\t\t共载"+sumweigth+"吨");
System.out.println("***租出车总价格为:"+sumprice*ff);
}
else{
System.out.println("退出系统!希望你下次能用租车系统!");
}
}
}
请问这一段是匿名内部类吗?
请问这里面的无参构造方法的作用是什么?
private被继承到子类后是无效的,子类可以进行修改使用
car类里面的属性用private会比较好(java封装的特性)