Java/Class&Object
클래스로 TV만들기 2
Bohemian life
2012. 4. 10. 13:28
class Tv2{
//멤버 변수
String color;
boolean power;
int channel;
//멤버 메소드 (동작수행)
public void power(){ //TV(on/off)
power =!power;
}
public void channelUp(){ //채널 Up
++channel;
}
public void channelDown(){ //채널Down
--channel;
}
}
public class TvTest2 {
public static void main(String[] args){
//객체선언 , 생성
Tv2 t= new Tv2();
t.power();
System.out.println("파워: " + t.power);
t.channel = 7; //채널지정
System.out.println("채널: " + t.channel);
//채널변경
t.channelUp();
System.out.println("채널: " + t.channel);
t.channelDown();
System.out.println("채널: " + t.channel);
t.power();
System.out.println("파워: " + t.power);
}
}
출력값 파워: true 채널: 7 채널: 8 채널: 7 파워: false