接口&异常&数组基础题

发布于:2025-04-08 ⋅ 阅读:(33) ⋅ 点赞:(0)

题目描述

设想你正在构建一个智能家居控制系统。这个系统可以连接多种不同类型的智能设备,如智能灯泡、智能空调和智能门锁。每种设备都有其独特的功能,不过它们也有一些通用的操作,像开启、关闭和获取设备状态等。系统需要提供一个方法来控制多个设备的开关,同时还要有一个方法依据用户输入的设备类型来创建对应的设备对象。

详细要求

  1. 定义接口:创建一个 SmartDevice 接口,其中包含开启设备、关闭设备以及获取设备状态的抽象方法。
  2. 实现具体设备类:创建 SmartBulb(智能灯泡)、SmartAirConditioner(智能空调)和 SmartLock(智能门锁)类,让它们实现 SmartDevice 接口。
  3. 多态的实现
    1. 方法形参:创建一个 controlDevices 方法,该方法接收一个 SmartDevice 数组作为参数,对数组里的所有设备执行开启或关闭操作。
    2. 方法返回值:创建一个 createDevice 方法,该方法依据用户输入的设备类型返回对应的 SmartDevice 对象。
  4. 异常处理:在 createDevice 方法中,若用户输入的设备类型不被支持,要抛出一个自定义异常 MyException

SmartDevice类

public interface SmartDevice {
    boolean open();
    boolean close();
    void condition();
}

SmartBlub类

public class SmartBulb implements SmartDevice{

    private boolean isOn;

    @Override
    public boolean open() {
        isOn = true;
        return isOn;
    }

    @Override
    public boolean close() {
        isOn = false;
        return isOn;
    }

    @Override
    public void condition() {
        if (isOn){
            System.out.println("智能灯泡已开启");
        }else {
            System.out.println("智能灯泡已关闭");
        }
    }
}

SmartAirCondition类

public class SmartAirConditioner implements SmartDevice{

    private boolean isOn;

    @Override
    public boolean open() {
        isOn = true;
        return isOn;
    }

    @Override
    public boolean close() {
        isOn = false;
        return isOn;
    }

    @Override
    public void condition() {
        if (isOn){
            System.out.println("智能空调已开启");
        }else {
            System.out.println("智能空调已关闭");
        }
    }
}

SmartLock类

public class SmartLock implements SmartDevice{

    private boolean isOn;

    @Override
    public boolean open() {
        isOn = true;
        return isOn;
    }

    @Override
    public boolean close() {
        isOn = false;
        return isOn;
    }

    @Override
    public void condition() {
        if (isOn){
            System.out.println("智能门锁已开启");
        }else {
            System.out.println("智能门锁已关闭");
        }
    }
}

ControlDevice类

public class ControlDevice {

    public void ControlDevice(SmartDevice [] smartDevices){
        for(SmartDevice smartDevice : smartDevices){
            smartDevice.open();
            smartDevice.condition();
            smartDevice.close();
            smartDevice.condition();
            System.out.println();
        }
    }

    public SmartDevice createDevice(String name) throws MyException{
        switch (name){
            case "智能灯泡":
                return new SmartBulb();
            case "智能空调":
                return new SmartAirConditioner();
            case "智能门锁":
                return new SmartLock();
            default:
                throw new MyException("不支持的设备类型: " + name);
        }
    }
}

MyException自定义异常类

public class MyException extends RuntimeException {

    public MyException(String message) {
        super(message);
    }

}

Test

public class Test {
    public static void main(String[] args) {

        try {
            ControlDevice controlDevice = new ControlDevice();

            SmartBulb bulb =(SmartBulb) controlDevice.createDevice("智能灯泡");
            SmartAirConditioner airConditioner =(SmartAirConditioner) controlDevice.createDevice("智能空调");
            SmartLock lock =(SmartLock) controlDevice.createDevice("智能门锁");

            SmartDevice [] smartDevice = {bulb,airConditioner,lock};
            controlDevice.ControlDevice(smartDevice);

            controlDevice.createDevice("智能冰箱");
        } catch (MyException e) {
            e.printStackTrace();
            System.out.println("该家具不存在!");
        }finally {
            System.out.println("程序运行完毕!");
        }


    }
}

输出结果:


网站公告

今日签到

点亮在社区的每一天
去签到