需要对灯光进行命名或以其他属性进行区分,可以使用字典或映射来存储和管理灯光。
例如:
Python 示例:class Light:
def turn_on(self):
# 打开灯光的代码
pass
def turn_off(self):
# 关闭灯光的代码
pass
lights = {}
def initialize_lights(names):
for name in names:
lights[name] = Light()
def turn_light_on(name):
if name in lights:
lights[name].turn_on()
def turn_light_off(name):
if name in lights:
lights[name].turn_off()
initialize_lights(["LivingRoom", "Bedroom", "Kitchen"])
turn_light_on("LivingRoom")
turn_light_off("Bedroom")
|