qq_N1名前変_0
2016-03-02 09:24
package com.imooc.model; public class Car { private int id;// 车的ID private String Carname;// 车的名字 private double price; // 车的价格 private String type; // 车的类型 private double stroke;// 车的行程 public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } public String getType() { return type; } public void setType(String type) { this.type = type; } public double getStroke() { return stroke; } public void setStroke(double stroke) { this.stroke = stroke; } @Override public String toString() { return " 编号 :" + id +" " + " 车名 :" + Carname + " " + " 价格:" + price + " " + " 类型 :" + type + " " +" 满油行程 :" + stroke; } public Car(int id, String carname, double price, String type, double stroke) { this.id = id; this.Carname = carname; this.price = price; this.type = type; this.stroke = stroke; } public void zaike() { } public void zaiwu() { } }
BUS:
package com.imooc.model; /* * 公交汽车 :载人 */ public class Bus extends Car { public Bus(int id, String carname, double price, String type, double stroke) { super(id, carname, price, type, stroke); // TODO Auto-generated constructor stub } }
PickupTrucks:
package com.imooc.model; /* * 皮卡车: 可载货载人 */ public class PickupTrucks extends Car{ public PickupTrucks(int id, String carname, double price, String type, double stroke) { super(id, carname, price, type, stroke); // TODO Auto-generated constructor stub } }
Truck:
package com.imooc.model; /* * 卡车 : 载货 */ public class Truck extends Car{ public Truck(int id, String carname, double price, String type, double stroke) { super(id, carname, price, type, stroke); // TODO Auto-generated constructor stub } }
Test:
package com.imooc.test; import java.util.Scanner; import com.imooc.model.Bus; import com.imooc.model.Car; import com.imooc.model.PickupTrucks; import com.imooc.model.Truck; public class test { public static void main(String[] args) { Car[] c = { new Bus(1, "公交汽车", 500.0, "载人车辆", 100), new Truck(2, " 货车 ", 400.0, "载物车辆", 200), new PickupTrucks(3, "皮卡车 ", 800.0, "既能载人又能载物", 400) }; System.out.println("欢迎来到滴滴租车!"); System.out.println("下一步请按1,走错请按0"); Scanner sca = new Scanner(System.in); int anniuid = sca.nextInt(); System.out.println(anniuid); if (anniuid == 1) { System.out.println("这里是滴滴租车的全部车辆信息:"); for (int i = 0; i < c.length; i++) { System.out.println(c[i]); } } else if (anniuid == 0) { System.out.println("确定要退出吗?确认退出摁 1,取消摁其他任意键"); Scanner sca1 = new Scanner(System.in); int t = sca1.nextInt(); if (t == 1) { System.exit(t); } else if (t != 1) { System.out.println("这里是滴滴租车的全部车辆信息:"); for (int i = 0; i < c.length; i++) { System.out.println(c[i]); } } } int i1 = 0; int i = 0; System.out.println("请选择你要租的车辆个数 : "); Scanner sca2 = new Scanner(System.in); int number = sca2.nextInt(); System.out.println("你需要租 " + number + "辆车"); System.out.println("请选择需要租的车辆的编号 : "); Scanner sca3 = new Scanner(System.in); for (i = 1; i <= number; i++) { System.out.println("第" + i + "辆车的编号是 :"); i1 = sca3.nextInt(); if (i1 > 3) { System.out.println("选择的编号不存在请重新选择(编号只能1~3之间) :"); } i1 = i - 1;// 获取当前选择车辆的信息标记 System.out.println("你选择了以下车辆 : " + c[i1]); } /*System.out.println("你需租借的车辆清单:"); for (i1 = 0; i1 < c.length; i1++) { System.out.println(c[i1]); System.out.println("当前租借车辆的价格 : " + c[i1].getPrice() + "当前租借车辆的行程 :" + c[i1].getStroke()); } i1 = i1 - 1;// 获取当前选择车辆的信息标记 int price1 = (int) c[i1].getPrice(); price1 = price1 + price1; System.out.println("当前的租借车辆的总价格是 : " + price1); */ } }
运行结果 :
学渣表示 只能写到 请问您是否要租车…………
和题目的要求不同啊(不同车型要有不同的属性),如果每个子类的属性和方法都和父类一样,为何要创建子类呢?
没人呢- -算了我晚上自己改把- -
写的时间比较仓促,只写了半小时。目测BUG不少于10个。。。
Java入门第二季 升级版
530655 学习 · 6091 问题
相似问题