본문 바로가기
Backend/JAVA

[JAVA] this와 this()

by howdyoon 2023. 3. 16.

this

- 인스턴스 자신을 가리키는 참조변수, 인스턴스의 주소가 저장되어 있다.
- 모든 인스턴스 메서드에 지역변수로 숨겨진 채로 존재한다.

 

this(), this(매개변수)

- 생성자, 같은 클래스의 다른 생성자를 호출할 때 사용한다.

- 한 생성자에서 다른 생성자를 호출할 때는 반드시 첫 줄에서만 호출이 가능하다.

 

- Score 클래스 생성 후 실습

package oop0316;

public class Test04_this {

	public static void main(String[] args) {

		Score one=new Score();
		one.disp();
		
		Score two=new Score("김연아", 50, 60, 70);
		two.disp();
		
		Score three=new Score("무궁화", 10, 20, 30);
		three.disp();
		
		//객체가 참조하고 있는 주소
		System.out.println(one.hashCode());
		System.out.println(two.hashCode());
		System.out.println(three.hashCode());
		/////////////////////////////////////
		
		Score kim  = new Score("봉선화", 10, 20, 30);  //#100
		Score lee  = new Score("라일락", 40, 50, 60);  //#200
		Score park = new Score("진달래", 70, 80, 90); //#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();
		
		int size=score.length;
		
		for(int i=0; i<size; i++) {
			score[i].disp();
		}//for end

	}//main() end
}//class end

- Score 클래스

package oop0316;

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);//손흥민
		System.out.println(this.kor);
		System.out.println(this.eng);
		System.out.println(this.mat);
		System.out.println(this.aver);
	}//disp() end
}//class end

 

package oop0317;

class Sungjuk {
	private String name;
	private int kor, eng, mat;
	private int aver;

	//기본 생성자 함수 default constructor
	public Sungjuk() {
		//생성자 함수도 자신의 다른 생성자 함수를 호출할 수 있다
		//Sungjuk("손흥민"); 에러
		this("손흥민"); 
	}//end
	
	public Sungjuk(String name) {
		this.name=name; //this.멤버변수=매개변수
	}//end
	
	public Sungjuk(int kor, int eng, int mat) {
		this("박지성");
		this.kor=kor;
		this.eng=eng;
		this.mat=mat;
		//this("박지성"); 에러. 자신의 생성자 함수를 호출하는 경우 첫줄에서 호출한다
	}//end
	
	public Sungjuk(int aver) {
		disp(); //생성자 함수에서 일반 메소드를 호출 가능하다
	}//end
	
	public void disp() {
		//this(85); 에러. 일반 메소드에서 생성자함수를 호출 불가능
	}//disp() end
	
}//class end


public class Test03_this {

	public static void main(String[] args) {
		//this()
		//->자신의 생성자 함수가 자신의 생성자 함수를 호출할때

	}//main() end
}//class end

'Backend > JAVA' 카테고리의 다른 글

[JAVA] final  (0) 2023.03.20
[JAVA] static  (0) 2023.03.16
[JAVA] String  (0) 2023.03.16
[JAVA] constructor  (0) 2023.03.15
[JAVA] class  (0) 2023.03.15