基于面向对象的简单系统
封装、继承、多态。抽象类、接口、异常
具体项目见:https://www.bilibili.com/video/BV16i4y157GW?p=32&spm_id_from=pageDriver
需求如下:
- 三类汽车:小轿车、大巴车、卡车,其中小轿车四座,大巴车五座,卡车两座
- 使用封装、继承和抽象来定义车辆
- 可以对车辆信息进行修改,
- 卡车载重不超过12吨,使用自定义异常来处理错误,小轿车和大巴车没有此功能,要求使用接口实现
具体实现:
- 父类Vehicle
package com.south.javase.demo01;
public abstract class Vehicle {
private String color;//颜色
private String name;//名字
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Vehicle(String color, String name) {
this.color = color;
this.name = name;
}
//不需要去实现的方法我们就可以加一个abstract 定义为抽象的,实现体去具体的类中描述即可
//而且抽象方法只能在抽象类中定义
public abstract String suitNum();
}
- 子类 Car
package com.south.javase.demo01;
//轿车类
public class Car extends Vehicle{
//带参构造函数
public Car(String color, String name) {
super(color, name);
}
@Override
public String suitNum() {
return "4座 ";
}
}
- 子类 Bus
package com.south.javase.demo01;
//大巴 53座
public class Bus extends Vehicle{
public Bus(String color, String name) {
super(color, name);
}
@Override
public String suitNum() {
return "53座";
}
}
- 子类 Truck
package com.south.javase.demo01;
//卡车 2座
public class Truck extends Vehicle implements Container {
private int weight;
public Truck(String color, String name,int weight) {
super(color, name);
this.weight=weight;
}
@Override
public String suitNum() {
return "2座 ";
}
@Override
public int getWeight() {
return this.weight;
}
}
- 自定义异常类 VehicleException
package com.south.javase.demo01;
public class VehicleException extends Exception{
public VehicleException(String error){
super(error);
}
}
- 接口 Container
package com.south.javase.demo01;
public interface Container {
public int getWeight();
}
- Test类
package com.south.javase.demo01;
import java.util.Scanner;
public class Test {
private static Scanner scanner;
private static Car car;
private static Bus bus;
private static Truck truck;
private static Vehicle[] vehicles;
static {
scanner= new Scanner(System.in);
car=new Car("黑色","汉兰达");
bus=new Bus("黑色","大巴");
truck=new Truck("棕色","卡车",20);
vehicles=new Vehicle[3];
vehicles[0]= car;
vehicles[1]= bus;
vehicles[2]= truck;
}
public void showVehicles(){
System.out.println("欢迎来到我们的汽车管理系统");
System.out.println("车辆名称"+"\t\t"+"车辆颜色"+"\t\t"+"座位数"+"\t\t"+"载重量");
for (Vehicle vehicle : vehicles) {
if (vehicle instanceof Truck){
Truck truck =(Truck)vehicle;
System.out.println(vehicle.getName()+"\t\t"+vehicle.getColor()+"\t\t"+vehicle.suitNum()+"\t\t"+truck.getWeight());
}else {
System.out.println(vehicle.getName()+"\t\t"+vehicle.getColor()+"\t\t"+vehicle.suitNum()+"\t\t无具体数据");
}
}
System.out.println("1.小轿车\t\t2.大巴车\t\t3.卡车");
System.out.println("请输入您想要修改的车辆");
int num=scanner.nextInt();
switch (num){
case 1:
update("car");
break;
case 2:
update("bus");
break;
case 3:
update("truck");
break;
default:
System.out.println("您输入的车辆不存在");
break;
}
System.out.println("欢迎来到我们的汽车管理系统");
System.out.println("车辆名称"+"\t\t"+"车辆颜色"+"\t\t"+"座位数"+"\t\t"+"载重量");
for (Vehicle vehicle : vehicles) {
if (vehicle instanceof Truck){
Truck truck =(Truck)vehicle;
System.out.println(vehicle.getName()+"\t\t"+vehicle.getColor()+"\t\t"+vehicle.suitNum()+"\t\t"+truck.getWeight());
}else {
System.out.println(vehicle.getName()+"\t\t"+vehicle.getColor()+"\t\t"+vehicle.suitNum()+"\t\t无具体数据");
}
}
}
public void update(String type) {
String name=null;
String color=null;
int weight=0;
if (type.equals("car")){
System.out.print("输入车辆名称:");
name=scanner.next();
System.out.print("请输入车辆颜色:");
color=scanner.next();
Car car = new Car(color,name);
vehicles[0]=car;
}
if (type.equals("bus")){
System.out.print("输入车辆名称:");
name=scanner.next();
System.out.print("请输入车辆颜色:");
color=scanner.next();
Bus bus = new Bus(color,name);
vehicles[1]=bus;
}
if (type.equals("truck")){
System.out.print("输入车辆名称:");
name=scanner.next();
System.out.print("请输入车辆颜色:");
color=scanner.next();
System.out.print("请输入车辆载重:");
weight=scanner.nextInt();
if (weight>12){
VehicleException vehicleException =new VehicleException("卡车载重不能大于12吨");
try {
throw vehicleException;
} catch (VehicleException e) {
e.printStackTrace();
return;
}
}else {
Truck truck = new Truck(color,name,weight);
vehicles[2]=truck;
}
}
System.out.println("================================");
showVehicles();
}
public static void main(String[] args) {
Test test = new Test();
test.showVehicles();
}
}