这是我参与11月更文挑战的第18天,活动详情查看:2021最后一次更文挑战
样例
题目需求
1.一个人去租车公司租车,车分为轿车、客车、电动车,轿车品牌有宝马(200)、奔驰(300)、奥拓(50)、客车品牌五菱(800)、长安(1500);电动车有雅迪(20)、艾玛(50)、阳光(80);根据租车类型、品牌、天数计算租车费用 2.每类车都需要维护,轿车维护主要是加油;客车维护是清洁,电动车维护是补胎。出租公司每天都要对车辆进行维护。轿车还需要定期保养
分析
1.有哪些类:轿车类、客车类、电动车类、宝马(车的品牌属性),车类、人类、公司类 2.类之间的关系:车类是父类,轿车、客车、电车是子类 3.每个类的属性:只要有继承关系,那么所有子类都有的属性放在父类,品牌、价格,子类还可以有自己的属性 4.每个类的行为:只要有继承关系,那么所有子类都有的行为,如果实现一样,放在父类;如果某个行为子类都有,但是实现不一样,那么写成抽象方法放到父类,子类实现
代码块
- 车类
/**
* Car类
*/
public abstract class Car {
private String name;
private String model;
private double price;
public Car() {
super();
// TODO Auto-generated constructor stub
}
public Car(String name, String model, double price) {
super();
this.name = name;
this.model = model;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public abstract void weihu();
}
复制代码
- 轿车类
/**
* 轿车类
*/
public class jiaoche extends Car{
public jiaoche() {
super();
}
public jiaoche(String name, String model, double price) {
super(name, model, price);
}
@Override
public void weihu() {
System.out.println(this.getModel()+this.getName()+"加油完毕");
}
}
复制代码
- 客车类
/**
* 客车类
*/
public class keche extends Car{
public keche() {
super();
}
public keche(String name, String model, double price) {
super(name, model, price);
}
@Override
public void weihu() {
System.out.println(this.getModel()+this.getName()+"清洁完毕");
}
}
复制代码
- 电动车类
/**
*电动车类
*/
public class diandongche extends Car{
public diandongche() {
super();
}
public diandongche(String name, String model, double price) {
super(name, model, price);
}
@Override
public void weihu() {
System.out.println(this.getModel()+this.getName()+"补胎完毕");
}
}
复制代码
- 公司类
/*
*公司类
*/
public class Company {
public Company() {
super();
}
public void weihu(Car car){
car.weihu();
}
}
复制代码
- 人类
/**
*人类
*/
public class Person {
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return name + ", 租的车:" + car.getModel()+car.getName()+car.getPrice() + "/天,租了"+day+"天";
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
private Car car;
private int day;
public Person(String name, Car car) {
super();
this.name = name;
this.car = car;
}
public Person() {
super();
}
public Person(String name) {
super();
this.name = name;
}
public Person(String name, Car car, int day) {
super();
this.name = name;
this.car = car;
this.day = day;
}
public void zuche(Car car){
System.out.println("需要花费:"+car.getPrice()*day);
}
}
复制代码
- 测试类
public class Test {
public static void main(String[] args) {
Company company = new Company();
Car[] car = {
new diandongche("电动车","雅迪",20),
new diandongche("电动车","艾玛",50),
new diandongche("电动车","阳光",80),
new keche("客车", "五菱", 800),
new keche("客车","长安",1500),
new jiaoche("轿车", "奥拓", 20),
new jiaoche("轿车", "奔驰", 300),
new jiaoche("轿车", "宝马", 200)
};
Person p1 = new Person("张三");
p1.setCar(car[1]);
p1.setDay(3);
System.out.println(p1);
p1.zuche(car[1]);
company.weihu(car[1]);
}
}
复制代码