반응형
class Tv {
boolean power;
int channel;
void power() { power = !power; } //부모의 멤버는 5개.
void channelup() { ++channel; }
void channeldown() { -- channel; }
}
class SmartTv extends Tv{
boolean caption;
void displayCaption(String text) { //자식의 멤버는 2개 + (부모 멤버 5개) = 7개
if (caption) {
System.out.println(text);
}
}
}
class Ex7 {
public static void main(String[] args) { //만약 부모멤버를 상속받지 않으면
SmartTv stv1 = new SmartTv(); // channel, channelup, channeldown 메서드 사용불가.
stv1.channel = 12;
stv1.channelup();
System.out.println(stv1.channel);
stv1.channeldown();
System.out.println(stv1.channel);
stv1.displayCaption("안녕하세요");
stv1.caption = false;
stv1.displayCaption("안녕하세요");
}
}
조상의 변경은 자손에 영향을 미치지만
자손의 변경은 조상에 영향을 미치지 않는다.
반응형
'코딩 하루 1문제 프로젝트' 카테고리의 다른 글
코딩테스트 모스부호(1) 문제 풀기. switch 문. (0) | 2023.03.23 |
---|---|
getter setter 예제 (0) | 2023.03.05 |
for문 예제 (0) | 2023.03.02 |
문자형 배열 문제 (0) | 2023.02.28 |
클래스 변수와 인스턴스 변수 (0) | 2023.02.27 |