面向对象题
本文最后更新于:2021年9月24日 下午
- 打车时可以打专车或快车。任何车都有车牌号和名称。快车每公里1元,专车每公里2元。行程开始时,显示车辆信息。行程结束时,显示打车金额。行程距离为5公里。
class Car{
constructor(licence,name){
this.licence = licence
this.name = name
}
}
class FastCar extends Car{
constructor(licence,name){
super(licence,name)
this.price =1
}
}
class PrivateCar extends Car{
constructor(licence,name){
super(licence,name)
this.price = 2
}
}
class Order{
constructor(car,distance){
this.car = car
this.distance = distance
}
getIn(){
console.log(this.car.name);
console.log(this.car.licence);
}
getOff(){
console.log(this.car.price*this.distance);
}
}
let car1 = new PrivateCar('粤A8888888','特斯拉')
let cost1 = new Order(car1,5).getOff()
- 某停车场, 分三层, 每层100 车位,每个车位都能监控到车辆的进入和离开,车辆进入前,显示每层的空余车位数量,车辆进入时,摄像头可识别车牌号和进入时间,车辆出来时,出口显示屏显示车牌号和停车时长
class Car{
constructor(licence){
this.licence = licence
}
}
// 整个停车场
class ParkingLot{
constructor(floors){
// floors:[[300],[300],[300]]
this.floors = floors
this.dataBase = []
}
showSpace(){
for(let i=0;i<this.floors.length;i++){
console.log(`第${i+1}层空闲车位(${this.floors[i].emptySpace()})`)
}
}
intoLot(car,floorIndex,lotIndex){
//车牌,时间
let licence = car.licence
let intoTime = Date.now()
let space = this.floors[floorIndex].lot[lotIndex]
this.dataBase[licence] = {intoTime,floorIndex,lotIndex}
space.checkIn()
}
outLot(car){
let outTime = Date.now()
let licence = car.licence
let info = this.dataBase[licence]
let space = this.floors[info.floorIndex].lot[info.lotIndex]
let timeCount = parseInt((outTime-info.intoTime)/36000000)
console.log(`停车时长${timeCount}小时`)
space.checkOut()
}
}
// 车位,监视驶入离开
class ParkingSpace{
constructor(){
this.free = true
}
checkIn(){
this.free = false
}
checkOut(){
this.free = true
}
}
// 层
class ParkingFloor{
constructor(floor,lot){
this.floor = floor
this.lot = lot
}
emptySpace(){
//返回空闲车位
let freeLot = 0
this.lot.forEach(item => {
if(item.free){
freeLot++
}
});
return freeLot
}
}
- 一副扑克牌有54张牌,其中52张是正牌,另2张是副牌(大王和小王)。
52张正牌又均分为13张一组,并以黑桃、红桃、草花、方块四种花色表示各组,每组花色的牌包括从1-10(1通常表示为A)以及J、Q、K标示的13张牌。const numbers = ['A', 1,2,3,4,5,6,7,8,9,'J','Q','K'] const signs = ['♥️', '♦️', '♣️', '♠️'] class Poker { constructor(signs,numbers) { this.signs = signs this.numbers = numbers this.mounts = 52 this.pokers = [] } initPokers(){ for(let i =0;i<this.signs.length;i++){ for(let j = 0;j<this.numbers.length;j++){ this.pokers.push(new Card(this.signs[i],this.numbers[j])) } } this.pokers.push(new Card('BLACK','GHOST')) this.pokers.push(new Card('RED','GHOST')) } shuffleCard() { // let arr = this.pokers // let len = arr.length // let temp,i // while(len){ // i = Math.floor(Math.random()*len--) // temp = arr[i] // arr[i] = arr[len] // arr[len] = temp // } // this.pokers = arr this.pokers = this.pokers.sort(()=>Math.random()-0.5) // Math.random() - 0.5 不完全随机 const temp = [...this.pokers] let len = temp.length while(len){ const i = Math.floor(Math.random() * len--) [temp[i],temp[len]] = [temp[len],temp[i]] } this.pokers = [...temp] } } class Card { constructor(sign, number) { this.sign = sign; this.number = number; } } let game1 = new Poker(signs,numbers) game1.initPokers() game1.shuffleCard() console.log(game1.pokers); game1.shuffleCard() console.log(game1.pokers);
面向对象题
http://yoursite.com/2022/02/24/面向对象的题/