java

상속 클래스

improve 2024. 1. 22. 12:39

package tt;

 

class Tv {

boolean power; // 전원(on/off)

int channel; // 채널

 

void power() { power = !power; } // 전원

void channelUp() { ++channel; } // 채널 증가

void channelDown() { --channel; } // 채널 감소

}

 

class CaptionTv extends Tv {

boolean caption; // caption 상태(on/off)

void displayCaption(String text) {

if (caption) { // caption 상태가 on(true)일때 text를 보여준다.

System.out.println(text);

}

}

}

 

class CaptionTvTest {

public static void main(String args[]) {

CaptionTv ctv = new CaptionTv();

ctv.channel = 10; // 부모 클래스에서 받아온다(상속)

ctv.channelUp(); // 부모 클래스에서 받아온다(상속)

System.out.println(ctv.channel);

 

ctv.displayCaption("Hello, World");

ctv.caption = true; // caption(자막) 기능을 켠다.

ctv.displayCaption("Hello, World"); // ĸ���� ȭ�鿡 ���� �ش�.

}

}

남궁성 책의 기본 예제이다.

상속의 기본개념이다.

extends를 사용해 부모의 클래스를 상속받는 개념이다.