상속
조상 클래스 - 부모(Parent)클래스, 상위 (Super)클래스, 기반(Base)클래스
자손 클래스 - 자식(Child)클래스, 하위(Sub) 클래스, 파생된(Derived) 클래스
예제
TV 리모콘(?) 만들기일까..
동작
//콘솔에서 명령어를 입력받는다.
//Power on , off
// channel up, down
// caption message display
// command not found mesage
조건
Parent class, Child class 로 구현할것.
/**
*
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
class Tv2{
boolean power = false;
int chanel;
void power(boolean power){
this.power = power;
if(power)
System.out.println("Power on");
else
System.out.println("Power off");
}
void channelUp(){ chanel ++; System.out.println("chanel Up:"+chanel);}
void channelDown(){ chanel--; System.out.println("chanel Down:"+chanel); }
}
class CationTv2 extends Tv2{
boolean caption;
void dispalyCation(String text){
//caption = !caption;
if(caption)
System.out.println("Cation is :"+text);
else
System.out.println("Cation Off");
}
void commandNotFound(String command){
System.out.println("Command not found:"+command);
}
}
class CationTvTest2{
public static void main(String args[]){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
CationTv2 ctv = new CationTv2();
String val = "on";
while (!val.equals("exit")){
if(val.equals("on")) ctv.power(true);
else if(val.equals("off"))
ctv.power(false);
else if(val.equals("cup"))
ctv.channelUp();
else if(val.equals("cdown"))
ctv.channelDown();
else if(val.equals("caption on")) {
ctv.caption = true;
ctv.dispalyCation("Hello Tv World");
}else if(val.equals("caption off")) {
ctv.caption = false;
ctv.dispalyCation("Cation off");
}else{
ctv.commandNotFound(val);
}
val = in.readLine();
}
ctv.power(false);
}catch (Exception e) {
// TODO: handle exception
}
}
}
댓글