๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ด๋ก /Study

this

by 6161990 2021. 7. 20.

๐Ÿ“Œ this

  • ๋ชจ๋“  ์ธ์Šคํ„ด์Šค์˜ ๋ฉ”์†Œ๋“œ์— ์ˆจ๊ฒจ์ง„ ์ฑ„ ์กด์žฌํ•˜๋Š” ๋ ˆํผ๋Ÿฐ์Šค๋กœ, ํ• ๋‹น๋œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค.
  • ์‚ฌ์šฉ ์ด์œ  : ์ž๊ธฐ ์ž์‹ ์˜ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ณ€์ˆ˜&๋ฉ”์„œ๋“œ(this.), ์ƒ์„ฑ์žํ•จ์ˆ˜(this())๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
  • ์—ญํ•  1. ์ธ์Šคํ„ด์Šค ์ž์‹ ์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜.
  • ์—ญํ•  2. this(): ์ƒ์„ฑ์ž์—์„œ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•จ.์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ์—์„œ ํ•œ ์ƒ์„ฑ์ž๊ฐ€ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ.
  • ์—ญํ•  3. ํ•จ์ˆ˜ ์‹คํ–‰์‹œ ์ „๋‹ฌ๋˜๋Š” ๊ฐ์ฒด์˜ ์ฃผ์†Œ๋ฅผ ์ž๋™์œผ๋กœ ๋ฐ›๋Š”๋‹ค.

๐Ÿ“ this์—ญํ• 1. ์ž์‹ ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ด. ๊ฐ™์€ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด๋„ ๋‘˜์˜ ์ธ์Šคํ„ด์Šค ์ฃผ์†Œ๋Š” ๋‹ค๋ฆ„(date & date2)

public class MyDateTest {

      public static void main(String[] args) {
      MyDate date = new MyDate(); // ์—ฌ๊ธฐ date 
       
      date.setYear(2019);
      date.setMonth(77);
      date.setDay(100);
    
      date.showDate();
    
      MyDate date2 = new MyDate(); // ์—ฌ๊ธฐ date2์˜ ์ฃผ์†Œ๋Š” ๋‹ค๋ฆ„. 
      date2.setYear(2002);  
    
      date2.showDate();
    }
}

 

 

 

๐Ÿ“ this์—ญํ• 2. ๊ฐ™์€ ํด๋ž˜์Šค์˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, ๋ฐ˜๋“œ์‹œ ์ฒซ๋ฒˆ์งธ ์ค„์—์„œ.

public class Person {

      String name;
    	int age;
    	
    	public Person() {
	 //age = 20 ; ๋ถˆ๊ฐ€. why? ๋‹ค๋ฅธ statement ์˜ฌ ์ˆ˜ ์—†์Œ. this๋กœ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, first statement์—ฌ์•ผ ํ•˜๊ธฐ๋•Œ๋ฌธ์—.
    	  this("์ด๋ฆ„์—†์Œ" ,1);  // ์—ฌ๊ธฐ์„œ ๋‹ค๋ฅธ Person ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœ. 
    	}
    	
    	public Person(String name, int age) {
    	  this.name = name;
          this.age = age;
    	}
    	
    	public void showInfo() {
    	  System.out.println(name+","+age);
    	}
      
      
        public static void main(String[] args) {
		
	      Person personNoName=new Person();
	      personNoName.showInfo();
	      	
	      Person personLee = new Person("Lee",20);
	      personLee.showInfo();
	      
    }
}

 

 

 


๐Ÿ“ this์—ญํ• 3. ์ž์‹ ์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” this

public class Person {

      String name;
    	int age;
    	
    	public Person() {
    		this("์ด๋ฆ„์—†์Œ" ,1);  
    	}
    	
    	public Person(String name, int age) {
    		this.name = name;
    		this.age = age;
    	}
    	
    	public void showInfo() {
    		System.out.println(name+","+age);
    	}
      
         public Person getSelf(){ // this๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์˜ ๋ฐ˜ํ™˜ํƒ€์ž…์€ ์ž๊ธฐ์ž์‹ 
           return this;
         }
      
      public static void main(String[] args) {
		
	     Person personNoName=new Person();
	     personNoName.showInfo();
	      	
	     Person personLee = new Person("Lee",20);
	     personLee.showInfo();
	     System.out.println(personLee);  //์ธ์Šคํ„ด์Šค ์ฃผ์†Œ๊ฐ’ ์ถœ๋ ฅ : chapter5.Person@2ff4acd0   
        
             Person p = personLee.getSelf();
             System.out.println(p); //์ธ์Šคํ„ด์Šค ์ฃผ์†Œ๊ฐ’ ์ถœ๋ ฅ : chapter5.Person@2ff4acd0   
        
             // ์ฃผ์†Œ ๊ฐ’  : ์ฐธ์กฐ๋ณ€์ˆ˜์™€ this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํž™ ๋ฉ”๋ชจ๋ฆฌ๋Š” ๊ฐ™๋‹ค. 
	}
    
}

 


๐Ÿ’ป ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•ด๋ณด๊ธฐ

 

public class House {
	//this? ์ž๊ธฐ ์ž์‹ ์˜ ํด๋ž˜์Šค ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ณ€์ˆ˜, ๋ฉ”์„œ๋“œ, ์ƒ์„ฑ์žํ•จ์ˆ˜๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ
	
	public int price;  //๊ฐ€๊ฒฉ
	public String dong;  //์•„ํŒŒํŠธ (๋™)
	public int size;  //ํ‰์ˆ˜ 
	public String kind;  //๋นŒ๋ผ, ์•„ํŒŒํŠธ 
	
	public House() {
		//this() : ์ƒ์„ฑ์ž ํ•จ์ˆ˜ 
		//์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด์—์„œ ์ž์‹ ์˜ ํด๋ž˜์Šค ๋‚ด์— ์žˆ๋Š” ๋‹ค๋ฅธ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค. 
		this(100,32,"๋™ํŒ๊ต","์˜คํ”ผ์Šคํ…”");  
//		price = 100;
//		size =32;
//		dong = "๋™ํŒ๊ต";
//		kind = "์•„ํŒŒํŠธ";
	}
	
	public House(int price) {
		//์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๊ธฐ ๋•Œ๋ฌธ์— ์ž๊ธฐ ์ž์‹  ํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” this๋ฅผ ๋ช…์‹œํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค.
		//this.price=price; //but ์ด๊ฒƒ์€ ํšจ์œจ์ด ๋–จ์–ด์ง
		this(price,32,"๋™ํŒ๊ต","๋นŒ๋ผ");  //this()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉ
	}
	
	public House(int price, int size) {
		this(price, size, "๋™ํŒ๊ต", "์ฃผ์ƒ๋ณตํ•ฉ");
	}
	public House(int price, int size, String dong) {
		this(price, size, dong, "์•„ํŒŒํŠธ");
	}
	
	public House (int price, int size, String dong, String kind) {
						//this. : ๋ณ€์ˆ˜, ๋ฉ”์†Œ๋“œ
		this.price = price;
		this.size = size;
		this.dong = dong;
		this.kind = kind;
	}

}

 

public class HouseConstructorTest {

	public static void main(String[] args) {
		House house1 = new House();
		System.out.println(house1.price+":"+house1.size+":"+house1.dong+":"+house1.kind);  //100:32:๋™ํŒ๊ต:์˜คํ”ผ์Šคํ…”
	
		House house2 = new House(300);
		System.out.println(house2.price+":"+house2.size+":"+house2.dong+":"+house2.kind); //300:32:๋™ํŒ๊ต:๋นŒ๋ผ
		
		House house3 = new House(600,40);
		System.out.println(house3.price+":"+house3.size+":"+house3.dong+":"+house3.kind); //600:40:๋™ํŒ๊ต:์ฃผ์ƒ๋ณตํ•ฉ

		House house4 = new House(800,50,"์„œ์ดˆ๋™");
		System.out.println(house4.price+":"+house4.size+":"+house4.dong+":"+house4.kind); //800:50:์„œ์ดˆ๋™:์•„ํŒŒํŠธ
		
		
		House house5 = new House(10000,50,"์„œ์ดˆ๋™","ํŽœํŠธํ•˜์šฐ์Šค");
		System.out.println(house5.price+":"+house5.size+":"+house5.dong+":"+house5.kind); //10000:50:์„œ์ดˆ๋™:ํŽœํŠธํ•˜์šฐ์Šค
	}

}