java oop 19 상속 예시

bitcogo·2022년 4월 15일
0
public class Oop19_inheritance2 {

    public static void main(String[] args) {

        SmartTv stv = new SmartTv();
        stv.channel = 10; //조상으로부터 상속받은 멤버
        stv.channelUp();  //조상으로부터 상속받은 멤버
        System.out.println(stv.channel);
        stv.displayCaption("안녕");//boolean caption의 초기값 false라서
        stv.caption = true;
        stv.displayCaption("안녕~~");
    }
}
class Tv3{
    boolean power; //전원 on/off
    int channel; //채널변경

    void power() {power = !power;}
    void channelUp() {++channel;}
    void channelDown() {--channel;}
}
class SmartTv extends Tv3{ //스마트tv는 캡션(자막) 보여주는 기능추가됨
    boolean caption;//자막 on/off
    void displayCaption(String text) {
        if(caption) {//자막 on일때만 자막(text)을 보여줌
            System.out.println(text);
        }
    }
}
profile
공부하고 기록하는 블로그

0개의 댓글