javascript 面向对象-面试题实例

/ 从设计到模式
  // 设计模式简介
  // 设计
  // 模式
  // 分开
  // 从设计到模式
    // 23种设计模式
      // 创建型
        // 工厂模式(工厂方法模式,抽象工厂模式,建造者模式)
        // 单例模式
        // 原型模式
      // 组合型

      // 行为型
        // 策略模式
        // 模板方法模式
        // 观察者模式
        // 迭代器模式
        // 职责连模式
        // 命令模式
        // 备忘录模式
        // 状态模式
        // 访问者模式
        // 中介者模式
        // 解释器模式
      //结构型
        // 适配器模式
        // 装饰器模式
        // 代理模式
        // 外观模式
        // 桥接模式
        // 组合模式
        // 享元模式

// 打车时,可以打专车或者快车。任何车都有车牌号和名称
// 不同车价格不同,快车每公里1元,专车每公里2元
// 行程开始时,显示车辆信息
// 行程结束时,显示打车金额(假定行程就5公里)

// 画出UML类型
// 用ES6语法写出该实例

// 车 -父类
class Car{
  constructor(number,name){
     this.number = number
    this.name = name
  }
}

// // 快车
class Kuaiche extends Car{
  constructor(number,name){
       super(number,name)
           this.price = 1
  }
}

// // 专车
class Zhuanche extends Car{
  constructor(number,name){
    super(number,name)
    this.price = 2
  }
}

// // 行程
class Trip{
  constructor(car){
    this.car = car
  }
  start(){
    console.log(`运行开始,名称:${this.car.name},车牌号:${this.car.number}`)
  }
  end(){
    console.log('运行结束,价格:'+ (this.car.price)*5)
  }
}

// 测试
// let car = new Kuaiche(100,"北京现代")
// let trip = new Trip(car)
// trip.start()
// trip.end()

// 某停车场,分3层,每层100车位
// 每个车位都能监控到车辆的驶入和离开
// 车辆进入前,显示每层的空余车位数
// 车辆进入时,摄像头可识别车牌号和时间
// 车辆出来是,出口显示车牌号和停车时长


// 车
class Car{
  constructor(num){
    this.num = num
  }
}
// 停车场
class Park{
  constructor(floors){
    this.floors = floors || []
    this.camera = new Camera()
    this.screen = new Screen()
    this.cartList = {} //存储
  }
  in(car){
//
    const info = this.camera.shot(car)
    const i = parseInt(Math.random()*100%100)
    const place = this.floors[0].places[i]
    place.in()
    info.place = place
    this.cartList[car.num] = info
    console.log(this.cartList)
  }
  out(car){
    const info = this.cartList[car.num]
    // 讲停车位清空
    const place = info.place
    place.out()
    // 显示时间
    this.screen.show(car,info.inTime)
    // 清空
    delete this.cartList[car.num]
  }
  emtpyNum(){
    return this.floors.map(floor =>{
      return `${floor.index} 层还有${floor.emptyPlaceNum()}个车位`
    }).join('\n')
  }
}

// 层次
class Floor{
  constructor(index,places){
    this.index =index
    this.places = places || []
  }
emptyPlaceNum(){
  let num = 0
  this.places.forEach(p => {
    if(p.empty){
      num = num + 1
    }
  })
  return num
  }

}

// 车位
class Place{
  constructor(){
    this.empty = true
  }
  in(){
    this.empty = false
  }
  out(){
    this.empty = true
  }
}

// 摄像头
class Camera{
  shot(car){
    return {
      num: car.num,
      inTime: Date.now()
    }
  }
}
// 出口显示器
class Screen{
  show(car,inTime){
    console.log("车牌号",car.num)
    console.log("停车时间", Date.now() - inTime)
  }
}


//初始化停车场
const floors = []
  for(let i = 0;i<3;i++){
    const places = []
    for(let j = 0;j<100;j++){
      places[j] = new Place()
  }
  floors[i] = new Floor(i+1,places)

}
const park = new Park(floors)

const car1 = new Car(100)
const car2 = new Car(200)
const car3 = new Car(300)

park.in(car1)
console.log(park.emtpyNum())
park.in(car2)
console.log(park.emtpyNum())

park.out(car1)
console.log(park.emtpyNum())
park.out(car2)
console.log(park.emtpyNum())

park.in(car3)
console.log(park.emtpyNum())
park.out(car3)
console.log(park.emtpyNum())


// 1 层还有99个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 1 层还有98个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 车牌号 100
// 停车时间 17

// 1 层还有99个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 车牌号 200
// 停车时间 3
// 1 层还有100个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 1 层还有99个车位
// 2 层还有100个车位
// 3 层还有100个车位

// 车牌号 300
// 停车时间 5
// 1 层还有100个车位
// 2 层还有100个车位
// 3 层还有100个车位

猜你喜欢

转载自www.cnblogs.com/sklhtml/p/9488549.html