웹개발 교육/Java
[33일] Java (23) - this
ewok
2022. 9. 13. 11:08
this는 클래스가 자신을 가리키는 대명사로 일반 지역 변수와 멤버 변수를 구분하기 위해 사용한다.
다시 말해, 객체 내부에서 인스턴스 멤버에 접근하기 위해 this를 사용한다.
보통 생성자와 메소드의 매개 변수 이름이 필드(멤버 변수)와 동일한 경우, 인스턴스 멤버인 필드임을 명시하고자 할 때 사용한다.
package oop0913;
class Score{
//멤버변수 field
private String name="손흥민";
private int kor, eng, mat;
private int aver;
//생성자함수 constructor
public Score(String name, int kor, int eng, int mat) {
//this.멤버변수 = 지역변수
this.name = name;
this.kor = kor;
this.eng = eng;
this.mat = mat;
}//end
//멤버함수 method
public void disp() {
//지역변수의 우선순위가 가장 높다
String name = "박지성";
System.out.println(name); //박지성
System.out.println(this.name); //손흥민 this.멤버변수
}//disp() end
}//class end
public class Test01_this {
public static void main(String[] args) {
// this
//-> 클래스가 자신을 가리키는 대명사
//-> 일반 지역 변수와 멤버 변수를 구분하기 위함
// this()
//-> 자신의 생성자 함수를 호출
Score one = new Score("김연아", 50, 60, 75);
one.disp();
}//main() end
}//class end
생성자 함수와 멤버함수의 name은 멤버 변수(필드)의 name과 이름이 같다. 생성자 함수와 멤버 변수의 name은 지역변수로 그 안에서만 사용이 가능하다.
따라서 disp()에서 name을 출력해보면 "박지성"이 나온다.
생성자함수와 멤버 함수에서 필드에 접근하려면 this를 사용하면 된다.
disp()에서 this.name을 출력해보면 필드에 name인 "손흥민"이 나온다.
package oop0913;
class Score{
//멤버변수 field
private String name="손흥민";
private int kor, eng, mat;
private int aver;
//생성자함수 constructor
//생성자함수를 오버로딩하면 기본생성자 함수는 자동으로 생성되지 않는다
//그래서 기본생성자함수는 수동으로 생성할 것을 추천!!
public Score() {}
public Score(String name, int kor, int eng, int mat) {
//this.멤버변수 = 지역변수
this.name = name;
this.kor = kor;
this.eng = eng;
this.mat = mat;
this.aver = (kor+eng+mat)/3;
}//end
//멤버함수 method
public void disp() {
//지역변수의 우선순위가 가장 높다
String name = "박지성";
System.out.println(name); //박지성
System.out.println(this.name); //손흥민 this.멤버변수
System.out.println(this.kor);
System.out.println(this.eng);
System.out.println(this.mat);
System.out.println(this.aver);
}//disp() end
}//class end
public class Test01_this {
public static void main(String[] args) {
// this
//-> 클래스가 자신을 가리키는 대명사
//-> 일반 지역 변수와 멤버 변수를 구분하기 위함
// this()
//-> 자신의 생성자 함수를 호출
Score one = new Score("김연아", 50, 60, 75);
one.disp();
Score two = new Score();
two.disp();
Score three = new Score("무궁화", 10, 20, 25);
three.disp();
//객체 지향 프로그램 특징 : 은폐, 캡슐화
//객체가 참조하고 있는 주소
System.out.println(one.hashCode());
System.out.println(two.hashCode());
System.out.println(three.hashCode());
//객체 생성(메모리 할당)의 여부 확인
System.out.println(one);
System.out.println(two);
System.out.println(three);
}//main() end
}//class end
세 개의 인스턴스를 생성하여 각각 참조변수 one, two, three에 그 주소값 담았다.
이 주소를 확인하려면 .hashCode()를 사용하면 된다.

그리고 인스턴스가 생성되어 메모리를 할당받았는지 여부를 알고 싶으면 참조변수를 출력하면 된다.

객체를 배열에 담아 객체를 만드는 것도 가능하다.
Score kim = new Score("봉선화", 10, 20, 30); //주소 100 이라 가정
Score lee = new Score("라일락", 10, 20, 30); //주소 200
Score park = new Score("진달래", 10, 20, 30); //주소 300
//객체 배열
Score [] score = {
new Score("오필승", 11, 22, 33)
,new Score("코리아", 44, 55, 66)
,new Score("대한민국", 77, 88, 99)
};
/*
+---------+---------+--------+
| #100 | #200 | #300 |
+---------+---------+--------+
score[0] score[1] score[2]
*/
score[0].disp();
score[1].disp();
score[2].disp();
for(int i=0; i<score.length; i++) {
score[i].disp();
}
}//main() end
}//class end
