慕瓜0408972
2019-07-05 21:22
一边看一边写,还是不懂u+=((Qiche)o).getram();是什么意思
括号里的(Qiche)是类型强转,o是他用for in遍历car数组里面的元素,getrom就是获取吨数的方法咯
父类
public class Car {
protected int xuhao;
protected String name;
protected int money;
public Car(int xuhao,String name,int money)
{
this.xuhao=xuhao;
this.name=name;
this.money=money;
}
public void leixing() {
System.out.println(xuhao+"\t"+name+"\t"+money+"元/天");
}
}
货车类
public class Huoche extends Car {
private int rom;
public Huoche(int xuhao,String name,int money,int rom)
{
super(xuhao,name,money);
this.rom=rom;
}
public void leixing(){
System.out.println(xuhao+"\t"+name+"\t"+money+"元/天"+"\t"+"载货"+rom+"吨");
}
public int getrom(){
return rom;
}
}
汽车类
public class Qiche extends Car {
private int ram;
public Qiche(int xuhao,String name,int money,int ram)
{
super(xuhao,name,money);
this.ram=ram;
}
public void leixing(){
System.out.println(xuhao+"\t"+name+"\t"+money+"元/天"+"\t"+"载人"+ram+"人");
}
public int getram(){
return ram;
}
皮卡类
}
public class Pika extends Car {
private int ram;
private int rom;
public Pika(int xuhao,String name,int money,int ram,int rom)
{
super(xuhao,name,money);
this.ram=ram;
this.rom=rom;
}
public void leixing(){
System.out.println(xuhao+"\t"+name+"\t"+money+"元/天"+"\t"+"载人"+ram+"载货"+rom+"吨");
}
public int getram(){
return ram;
}
public int getrom(){
return rom;
}
}
主类
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Main kehu = new Main();
Car[] xinghao = new Car[6];
xinghao[0] =new Qiche(1,"奥迪A4",500,4);
xinghao[1] =new Qiche(2,"马自达6",400,4);
xinghao[2] =new Pika(3,"皮卡雪6",450,4,2);
xinghao[3] =new Qiche(4,"金龙",800,20);
xinghao[4] =new Huoche(5,"松花江",400,4);
xinghao[5] =new Huoche(6,"依维柯",1000,20);
System.out.println("欢迎使用答答租车系统");
System.out.println("您是否要租车:1是"+"\t"+"2否");
Scanner in =new Scanner(System.in);
int i = in.nextInt();
if(i==1)
{
System.out.println("您可租车的类型及其价格表");
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");
xinghao[0].leixing();
xinghao[1].leixing();
xinghao[2].leixing();
xinghao[3].leixing();
xinghao[4].leixing();
xinghao[5].leixing();
System.out.println("请选择您要租的汽车数量");
int j = in.nextInt();
Car[] f = new Car[j];
for(int s=0;s<j;s++)
{
System.out.println("请输入第"+(s+1)+"辆车的序号:");
int c=in.nextInt();
f[s]=xinghao[c-1];
}
System.out.println("请输入租车天数:");
int d=in.nextInt();
int u=0;
System.out.println("您的账单有:");
System.out.println("**********载人车有:");
for(Car o:f)
{
if(o.xuhao==1||o.xuhao==2||o.xuhao==4)
{
System.out.print(o.name);
u+=((Qiche)o).getram();
}else if(o.xuhao==3)
{
System.out.print(o.name);
u+=((Pika)o).getram();
}
}
System.out.println("总载客人数"+u+"人");
System.out.println("\n**********载货车有:");
int h =0;
for(Car y:f)
{
if(y.xuhao==5||y.xuhao==6)
{
System.out.print(y.name);
h+=((Huoche)y).getrom();
}else if(y.xuhao==3)
{
System.out.print(y.name);
h+=((Pika)y).getrom();
}
} System.out.println("总载货数"+h+"吨");
double qian=0;
for(Car e:f){
qian+=e.money;
}
qian=qian*d;
System.out.print("\n*****租车总金额:"+qian);
}
else
{
System.out.println("欢迎下次使用");
}
}
}
Java入门第二季 升级版
530636 学习 · 6091 问题
相似问题