예습/code
상속
밀김
2023. 1. 13. 10:31
728x90
public class Test {
public static void main(String[] args) {
SmartTv stv = new SmartTv();
stv.channel = 10;
// stv.caption = true;
stv.channelUp();
System.out.println(stv.channel);
stv.displayCaption("안뇽");
}
}
class Tv{
boolean power; //참,거짓
int channel;
//클래스(2개)
void power() { power = !power; }
void channelUp() { ++channel; } //channelUp을 호출하면 채널이 1씩 올라감
void channelDown() { --channel; }
//매서드(3개)
}
class SmartTv extends Tv{ //부모 Tv의 멤버를 상속받는 자손 SmartTv
// boolean caption;
//클래스
void displayCaption(String text) {
//매서드. string(문자열 형태로 반환) 그에 대한 변수 이름은 text
// if (caption) {
// System.out.println(text);
// }
System.out.println(text); //굳이 if문에 넣는 이유가 머지
}
}
보기쉽게 간단하게 만든 상속 인데..실행되는 순서 잘보면 좋을듯
public class Test {
public static void main(String[] args) {
b m = new b();
m.x = 1;
m.channelUp();
System.out.print(m.x);
m.ddtext("티비가 켜졋어용");
}
}
class a{
int x;
void channelUp() { ++x; }
void channelDown() { --x; }
}
class b extends a{
String t;
void ddtext(String t) {
System.out.println(t);
}
}
728x90