📅  最后修改于: 2023-12-03 15:42:14.649000             🧑  作者: Mango
这是一场GATE MOCK 2017的考试,第45道题目。
你需要设计一个程序,用于模拟门的开闭状态。门有三种状态:打开,关闭,和锁定。门有两种操作:打开和关闭。在门打开的状态下,只有关闭操作是有效的;在门关闭的状态下,只有打开操作是有效的。当门被锁定时,无论进行何种操作都是无效的。
你需要实现以下操作:
open()
:打开门,若门当前状态为关闭,则将门状态改为打开;否则操作无效。close()
:关闭门,若门当前状态为打开,则将门状态改为关闭;否则操作无效。lock()
:锁定门,将门状态改为锁定。若门当前状态为打开,则关闭门后再锁定;若门当前状态为关闭,则直接锁定门;若门当前状态为锁定,则操作无效。unlock()
:解锁门,将门状态改为关闭。若门当前状态为锁定,则将门状态改为关闭;否则操作无效。你需要实现一个 Gate
类来完成以上操作。
gate = Gate()
assert gate.get_status() == 'CLOSED'
gate.open()
assert gate.get_status() == 'OPEN'
gate.open()
assert gate.get_status() == 'OPEN'
gate.lock()
assert gate.get_status() == 'LOCKED'
gate.open()
assert gate.get_status() == 'LOCKED'
gate.unlock()
assert gate.get_status() == 'CLOSED'
class Gate:
def __init__(self):
pass
def open(self):
"""
Opens the gate.
If the gate is closed, the gate is opened. If the gate is open,
the call is ignored. If the gate is locked, the call is ignored.
"""
pass
def close(self):
"""
Closes the gate.
If the gate is open, the gate is closed. If the gate is closed,
the call is ignored. If the gate is locked, the call is ignored.
"""
pass
def lock(self):
"""
Locks the gate.
If the gate is open, close the gate and then lock it. If the gate is
closed, lock it directly. If the gate is locked, the call is ignored.
"""
pass
def unlock(self):
"""
Unlocks the gate.
If the gate is locked, unlock the gate and closed it. If the gate
is open or closed, the call is ignored.
"""
pass
def get_status(self) -> str:
"""
Returns the current status of the gate.
One of "OPEN", "CLOSED", or "LOCKED".
"""
pass
以上就是题目的内容及接口介绍,考生需要按照接口要求完成程序的实现。