class AirCondition: def init(self, brand, color, power, temperature): self.brand = brand self.color = color self.power = power self.switch_status = False self.temperature = temperature

def switch_on(self):
    self.switch_status = True

def switch_off(self):
    self.switch_status = False

def increase_temperature(self):
    self.temperature += 1

def decrease_temperature(self):
    self.temperature -= 1

def print_status(self):
    if self.switch_status:
        status = "开启"
    else:
        status = "关闭"
    print("空调状态:", status)
    print("设定温度:", self.temperature, "度")

创建airCondition对象

ac = AirCondition("格力", "白色", "2 匹", 25)

打开空调

ac.switch_on()

调整温度为20度

ac.decrease_temperature()

打印空调状态和设定温度

ac.print_status(

设计一个空调类 airCondition其中包括:1数据成员:品牌、颜色、功率、开关状态、设定温度;2构造函数:对品牌、颜色、功率、设定温度赋初值;3成员函数:切换开关状态、升温、降温。要求在 main 函数中创建一个 airCondition 的对象具体信息为:格力、白色、2 匹、25 度。调用其切换开关状态函数打开空调调用其降温函数调整温度为 20 度并打印空调状态和目前设定的温度到屏幕

原文地址: http://www.cveoy.top/t/topic/ispm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录