JAVASCRIPT

JAVASCRIPT ) 인터페이스란?

Hweb 2024. 10. 31. 10:18

인터페이스는 여러 객체들을 연결하는 대시보드

인터페이스는 복제가 가능

따라서 객체 - Class

 

 

자동차의 엔진과 창문을 만들고 이것들을 인터페이스에서 관리하고 자동차를 만드는 코드

class Engine {
    constructor(id){
        this.id = id;
        this.status = '';
    }
    run(speed){
        if(this.status == "On"){
            document.write(`속도: ${speed}Km/h<br>`);
        } else {
            document.write("시동을 거세요.");
        }
    }
    on(){this.status = "On";}
    off(){this.status = "Off";}
}

class Window{
    constructor(id){
        this.id = id;
    }
    up(){document.write("창문이 닫혔습니다.")}
    down(){document.write("창문이 열렸습니다.")}
}

class Interface {
    constructor(id){
        this.id = id;
        this.engine = new Engine(this.id);
        this.window = new Window(this.id);
    }
    engineOn(){this.engine.on();}
    accel(speed){this.engine.run(speed);}
    windowUp(){this.window.up();}
    windowDown(){this.window.down();}
}

class Car {
    constructor(id){
        this.id = id;
        this.dashboard = new Interface(this.id);
    }
}
const car = new Car('car')
car.dashboard.engineOn();
car.dashboard.windowDown();
car.dashboard.accel(90);

 

반응형