JAVA—出租车公司租车
样例
题目需求
1.一个人去租车公司租车,车分为轿车、客车、电动车,轿车品牌有宝马(200)、奔驰(300)、奥拓(50)、客车品牌五菱(800)、长安(1500);电动车有雅迪(20)、艾玛(50)、阳光(80);根据租车类型、品牌、天数计算租车费用
2.每类车都需要维护,轿车维护主要是加油;客车维护是清洁,电动车维护是补胎。出租公司每天都要对车辆进行维护。轿车还需要定期保养
分析
1.有哪些类:轿车类、客车类、电动车类、宝马(车的品牌属性),车类、人类、公司类
2.类之间的关系:车类是父类,轿车、客车、电车是子类
3.每个类的属性:只要有继承关系,那么所有子类都有的属性放在父类,品牌、价格,子类还可以有自己的属性
4.每个类的行为:只要有继承关系,那么所有子类都有的行为,如果实现一样,放在父类;如果某个行为子类都有,但是实现不一样,那么写成抽象方法放到父类,子类实现
代码块
- 车类
/*** Car类*/publicabstractclassCar { privateStringname; privateStringmodel; privatedoubleprice; publicCar() { super(); // TODO Auto-generated constructor stub } publicCar(Stringname, Stringmodel, doubleprice) { super(); this.name=name; this.model=model; this.price=price; } publicStringgetName() { returnname; } publicvoidsetName(Stringname) { this.name=name; } publicStringgetModel() { returnmodel; } publicvoidsetModel(Stringmodel) { this.model=model; } publicdoublegetPrice() { returnprice; } publicvoidsetPrice(doubleprice) { this.price=price; } publicabstractvoidweihu(); }
- 轿车类
/*** 轿车类*/publicclassjiaocheextendsCar{ publicjiaoche() { super(); } publicjiaoche(Stringname, Stringmodel, doubleprice) { super(name, model, price); } publicvoidweihu() { System.out.println(this.getModel()+this.getName()+"加油完毕"); } }
- 客车类
/*** 客车类*/publicclasskecheextendsCar{ publickeche() { super(); } publickeche(Stringname, Stringmodel, doubleprice) { super(name, model, price); } publicvoidweihu() { System.out.println(this.getModel()+this.getName()+"清洁完毕"); } }
- 电动车类
/***电动车类*/publicclassdiandongcheextendsCar{ publicdiandongche() { super(); } publicdiandongche(Stringname, Stringmodel, doubleprice) { super(name, model, price); } publicvoidweihu() { System.out.println(this.getModel()+this.getName()+"补胎完毕"); } }
- 公司类
/**公司类*/publicclassCompany { publicCompany() { super(); } publicvoidweihu(Carcar){ car.weihu(); } }
- 人类
/***人类*/publicclassPerson { publicintgetDay() { returnday; } publicvoidsetDay(intday) { this.day=day; } publicStringtoString() { returnname+", 租的车:"+car.getModel()+car.getName()+car.getPrice() +"/天,租了"+day+"天"; } publicCargetCar() { returncar; } publicvoidsetCar(Carcar) { this.car=car; } publicStringgetName() { returnname; } publicvoidsetName(Stringname) { this.name=name; } privateStringname; privateCarcar; privateintday; publicPerson(Stringname, Carcar) { super(); this.name=name; this.car=car; } publicPerson() { super(); } publicPerson(Stringname) { super(); this.name=name; } publicPerson(Stringname, Carcar, intday) { super(); this.name=name; this.car=car; this.day=day; } publicvoidzuche(Carcar){ System.out.println("需要花费:"+car.getPrice()*day); } }
- 测试类
publicclassTest { publicstaticvoidmain(String[] args) { Companycompany=newCompany(); Car[] car= { newdiandongche("电动车","雅迪",20), newdiandongche("电动车","艾玛",50), newdiandongche("电动车","阳光",80), newkeche("客车", "五菱", 800), newkeche("客车","长安",1500), newjiaoche("轿车", "奥拓", 20), newjiaoche("轿车", "奔驰", 300), newjiaoche("轿车", "宝马", 200) }; Personp1=newPerson("张三"); p1.setCar(car[1]); p1.setDay(3); System.out.println(p1); p1.zuche(car[1]); company.weihu(car[1]); } }