题目一
分析:
打车时,可以打专车和快车,热河车都有车牌号和名称 从这句话可以得出需要定义一个Car类 属性有车牌号和名称,快车和专车继承这个类
不同的车价格不同,快车每公里一元,专车每公里2元 说明每个子类都有自己的price属性 而且是不同的
行程开始显示车辆信息,行程结束,相识打车金额(假定行程就5公里) 这里我们可以再定义一个Trip 类 行为有 start 和 end
画UML类图
代码编写
class Car {
constructor(carId, carName) {
this.carId = carId
this.carName = carName
}
}
class Kuaiche extends Car {
constructor(carId, carName) {
super(carId, carName)
this.price = 1
}
}
class Zhuanche extends Car {
constructor(carId, carName) {
super(carId, carName)
this.price = 5
}
}
class Trip {
constructor(car) {
this.car = car
}
start() {
console.log(`行程开始,车辆名称:${this.car.carName}.车牌号:${this.car.carId}`)
}
end() {
console.log(`行程结束,价格:${this.car.price * 5}`)
}
}
let car = new Kuaiche(13551, '法拉利')
let trip = new Trip(car)
console.log(trip.start())
console.log(trip.end())
let car1 = new Zhuanche(13552, '宝马')
let trip1 = new Trip(car1)
console.log(trip1.start())
console.log(trip1.end())
题目二
分析:
莫停车场,分三层,每层100车位 这边可以定义三个类 Park,Floor,Place
每个车位都能监控到车辆的驶入和离开 可以看出 车位有连个行为 驶入(in)和离开(out)
车辆进入前,显示每层的空余车位数量 Park有一个显示空余车位的行为
车辆进入时,摄像头可识别车牌号和时间 这里可以看出还可以建立一个摄像头的类 并且 Park 有监听车辆进入的行为
车辆出来时,出口显示器显示车牌号和停车时长 需要定义一个显示器类(显示器有显示的行为) 并且Park有监听车辆出来的行为
UML类图
代码实现
class Park {
constructor(floors, camera, screen) {
this.floors = floors
this.camera = camera
this.screen = screen
this.carList = {}
}
emptyNum() {
let str = this.floors.map(floor => {
return `第${floor.index}层还要${floor.showEmptyNum()}个空位.`
}).join('\n')
console.log(str)
}
in(car) {
let info = this.camera.shot(car)
let floor_index = Math.floor(Math.random() * 3)
let place_index = Math.floor(Math.random() * 100)
let place = this.floors[floor_index].places[place_index]
place.in()
info.place = place
this.carList[car.carId] = info
console.log(info)
}
out(car) {
let info = this.carList[car.carId]
console.log(info)
this.screen.show(car, info.inTime)
info.place.out()
delete this.carList[car.carId]
}
}
class Floor {
constructor(index, places) {
this.places = places
this.index = index
}
showEmptyNum() {
let emptyNum = 0
this.places.forEach(item => {
if (item.empty) {
emptyNum++;
}
})
return emptyNum;
}
}
class Place {
constructor(empty = true) {
this.empty = empty
}
in() {
this.empty = false
}
out() {
this.empty = true
}
}
class Screen {
constructor() {
}
show(car, inTime) {
let now_date = new Date()
console.log(`车牌号:${car.carId},停车时长:${now_date.getTime() - inTime.getTime()}`)
}
}
class Camera {
constructor() {
}
shot(car) {
console.log(`${car.carId}进入停车场`)
return {
inTime: new Date(),
carId: car.carId
}
}
}
class Car {
constructor(carId) {
this.carId = carId
}
}
let floors = []
for (let i = 0; i < 3; i++) {
let places = []
for (let j = 0; j < 100; j++) {
let place = new Place()
places.push(place)
}
let floor = new Floor(i + 1, places)
floors.push(floor)
}
let camera = new Camera()
let screen = new Screen()
let park = new Park(floors, camera, screen)
console.log(park)
let car1 = new Car(43243)
let car2 = new Car(7857)
let car3 = new Car(110)
park.emptyNum()
park.in(car1)
park.emptyNum()
park.out(car1)
park.emptyNum()
park.in(car2)
park.emptyNum()
park.in(car3)
park.out(car3)
park.out(car2)
设计原则 小既是美