TV tv = new TV(10,"blue");
TV tv2 = new TV(20,"blue");
System.out.println(tv.getSize() + "인치 입니다");
System.out.println(tv.getColor() + "색 입니다");
tv.show(); // 10 인치 bule 색 입니다.
tv.compareSize(tv); // 내가 작습니다.
tv2.compareSize(tv); // 내가 큽니다.
public class TV {
int size;
String color;
public TV(int size, String color) {
this.size = size;
this.color = color;
this.show();
}
int getSize() {
return size;
}
String getColor() {
return color;
}
void compareSize(TV compareTV) {
//ㄴ엄연한 생성자
if (this.size > compareTV.size) {
System.out.println("작은 사이즈");
}
else if (this.size < compareTV.size) {
System.out.println("큰 사이즈");
}
else {
System.out.println("같은 사이즈");
}
}
private void show() {
System.out.println(this.size + "인치 "
+ this.color + "색 입니다.");
// System.out.println(getSize() + "인치 "
// + getColor() + "색 입니다.");
}
}
- this
객체 자신을 가리키는 레퍼런스 변수로, 자신의 객체에 접근할 때 사용된다.- this()
같은 클래스에서 생성자가 다른 생성자를 호출할 때 사용된다.
- this
주로 멤버변수와 매개변수의 이름이 동일할 때, 이를 구분하기 위해 사용된다.- this()
주로 코드의 중복을 줄일 목적으로 사용된다.
this()는 생성자 코드에서만 사용할 수 있다.
this()는 생성자 코드 안에서 사용될 때 첫번째 문장으로 다른 코드보다 가장 윗줄에 위치해야 한다.
다른 객체에게 자신의 정보를 숨기고 자신의 연산만을 통해 접근을 허용하는 것으로, 클래스 외부에서 특정 정보에 접근을 막는다는 의미이다.
5. 1번에서 tv.compareSize(tv); 에 대하여, 메모리 그림을 그리시오.