Java/Class&Object

클래스로 TV만들기

Bohemian life 2012. 4. 10. 13:28
class Tv{
	//멤버필드
	String color; //색상 (컬러/흑백)    //기본값 : null
	boolean power;//전원상태 (on/off)  //기본값 : false
	int channel;  //채널                //기본값 : 0
}

public class TvTest {
	public static void main(String[] args){
		//객체 선언, 객체 생성
		Tv t= new Tv();
		//Tv :참조자료형
		//t  :참조변수 (객체의 주소[reference] 저장)
		//new:객체 생성를 생성하도록 명령하는 연산자

		System.out.println(t.color);
		System.out.println(t.power);
		System.out.println(t.channel);

		//멤버변수에 데이터 할당
		t.color = "컬러";
		t.power = true;
		t.channel = 9;

		System.out.println(t.color + "TV 사용");
		System.out.println(t.power+": TV를 보다");
		System.out.println(t.channel+"번 채널 시청");

	}
}

null

false
0
컬러TV 사용
true: TV를 보다
9번 채널 시청