한모로그 2023. 1. 21. 11:58

추상 메소드

  • 리턴 타입, 메소드명, 매개변수만 기술되고 중괄호 { }를 붙이지 않는 메소드
  • public abstract를 생략하더라도 컴파일 과정에서 자동으로 붙음
  • 추상 메소드는 객체 A가 인터페이스를 통해 어떻게 메소드를 호출할 수 있는지 방법을 알려주는 역할


    출처, 이것이 자바다


package ch08.sec04;

//인터페이스
public interface RemoteControl {

    //상수필드. 컴파일시 public static final
    int MAX_VOLUME = 10;
    int MIN_VOLUME = 0;

    //추상메소드(미완성 메소드). 컴파일시 public abstract
    void turnOn();
    void turnOff();
    void setVolume(int volume);
}

package ch08.sec04;

//구현클래스
public class Television implements RemoteControl {

    //필드
    private int volume;

    @Override
    public void turnOn() {
        System.out.println("TV를 켭니다.");

    }

    @Override
    public void turnOff() {
        System.out.println("TV를 끕니다.");

    }

    @Override
    public void setVolume(int volume) {
        if(volume > RemoteControl.MAX_VOLUME) {
            this.volume = RemoteControl.MAX_VOLUME;
        }else if(volume < RemoteControl.MIN_VOLUME) {
            this.volume = RemoteControl.MIN_VOLUME;
        }else {
            this.volume = volume;
        }
        System.out.println("현재 TV 볼륨: " + this.volume);
    }

}

package ch08.sec04;

//구현클래스
public class Audio implements RemoteControl {

    //필드
    private int volume;

    //구현(재정의) : 완성메소드
    @Override
    public void turnOn() {
        System.out.println("Audio를 켭니다.");

    }

    @Override
    public void turnOff() {
        System.out.println("Audio를 끕니다.");

    }

    //private int volume; 필드의 setter메소드 기능
    @Override
    public void setVolume(int volume) {
        if(volume > RemoteControl.MAX_VOLUME) {
            this.volume = RemoteControl.MAX_VOLUME;
        }else if(volume < RemoteControl.MIN_VOLUME) {
            this.volume = RemoteControl.MIN_VOLUME;
        }else {
            this.volume = volume;
        }
        System.out.println("현재 Audio 볼륨: " + this.volume);
    }

}

package ch08.sec04;

public class Aircon implements RemoteControl {

    private int volume;

    @Override
    public void turnOn() {
        System.out.println("Aircon을 켭니다.");

    }

    @Override
    public void turnOff() {
        System.out.println("Aircon을 끕니다.");

    }

    @Override
    public void setVolume(int volume) {
        if(volume > RemoteControl.MAX_VOLUME) {
            this.volume = RemoteControl.MAX_VOLUME;
        }else if(volume < RemoteControl.MIN_VOLUME) {
            this.volume = RemoteControl.MIN_VOLUME;
        }else {
            this.volume = volume;
        }
        System.out.println("현재  Aircon 온도: " + this.volume);
    }

}

package ch08.sec04;

public class RemoteControlExample {

    public static void main(String[] args) {

        RemoteControl rc;

        //부모인터페이스 변수에 대입. 자동형변환
//        rc = new Television();
//        rc.turnOn();
//        rc.setVolume(5);
//        rc.turnOff();

        //System.out.println();

        //부모인터페이스 변수에 대입. 자동형변환
//        rc = new Audio();
//        rc.turnOn();
//        rc.setVolume(5);
//        rc.turnOff();

        //System.out.println();

//        rc = new Aircon();
//        rc.turnOn();
//        rc.setVolume(7);
//        rc.turnOff();


        remoteControl(new Television());
        remoteControl(new Audio());
        remoteControl(new Aircon());
    }

    //매개변수 다형성 : 중복된 코드를 줄이고, 유지보수 작업시 수정작업이 용이하게 됨.
    public static void remoteControl(RemoteControl rc) {

        int volume = 0;
        if(rc instanceof Television) {
            volume = 5;
        }else if(rc instanceof Audio) {
            volume = 7;
        }else if(rc instanceof Aircon) {
            volume = 9;
        }

        rc.turnOn();
        rc.setVolume(volume);
        rc.turnOff();
    }
}