知向前端
js 设计模式面试题之打车
2021-1-28 Jon
题目

打车时,可以打专车或者快车。任何车都有车牌号和名称。
不同车价格不同,快车每公里1元,专车每公里2元。
行程开始时,显示车辆信息
行程结束时,显示打车金额(假定行程就5公里)
画出UML类图 用ES6语法写出该示例



解答



  1. UML 类图
    uml类图




  2. 代码演示





class Trip {
constructor(car) {
this.car = car;
this.distance = 5;
}
start() {
console.log(`行程开始,名称: ${this.car.name}, 车牌号: ${this.car.number}`);
}
end() {
console.log('行程结束,价格: ' + (this.car.price * this.distance));
}
}
class Car {
constructor(name, number) {
this.name = name;
this.number = number;
}
}
class Zhuanche extends Car {
constructor(name, number) {
super(name, number);
this.price = 5;
}
}
class Kuaiche extends Car {
constructor(name, number) {
super(name, number);
this.price = 2;
}
}

// 测试
let car1 = new Zhuanche('A', '001');
let car2 = new Kuaiche('B', '002');

let trip1 = new Trip(car1);
let trip2 = new Trip(car2);

trip1.start();
trip1.end();
trip2.start();
trip2.end();

评论:
磁选机
2024-08-03 14:02 回复
orvheb01953PK-作者的见解深刻,让我对XX问题有了新的思考方向。www.well-techmachine.cn/chenggonganli/38.html
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容