js 设计模式面试题之打车

2021-1-28 Jon js+jquery+ajax

题目

打车时,可以打专车或者快车。任何车都有车牌号和名称。
不同车价格不同,快车每公里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();

标签: 面试题 设计模式

分享这篇文章
赞助鼓励:如果觉得内容对您有所帮助,您可以支付宝(左)或微信(右):

声明:如无特殊注明,所有博客文章版权皆属于作者,转载使用时请注明出处。谢谢!

发表评论:

皖ICP备15010162号-1 ©2015-2022 知向前端
qq:1614245331 邮箱:13515678147@163.com Powered by emlog sitemap