Schatzi Java Online Manual & Tutorial & Lecture

 1 this¶õ
 

ÀÚ¹Ù¿¡¼­´Â ÀϹÝÀûÀ¸·Î °´Ã¼ÀÇ ¸Þ¼Òµå ³»¿¡¼­ °´Ã¼°¡ °¡Áö°í ÀÖ´Â º¯¼ö ¶Ç´Â ¸Þ¼Òµå¸¦ Á÷Á¢ ÂüÁ¶ÇÒ ¼ö ÀÖ½À´Ï´Ù. ±×·±µ¥, ÀÚ¹Ù¿¡¼­ Ŭ·¡½º¸¦ Á¤ÀÇÇÏ°í °´Ã¼¸¦ »ý¼ºÇÒ ¶§, this¶ó´Â Ű¿öµå¸¦ ÀÚÁÖ »ç¿ëÇÏ°Ô µË´Ï´Ù. ±×·¸´Ù¸é this´Â ¾î¶² ¿ªÇÒÀ» Çϵµ·Ï ÀÚ¹Ù¿¡¼­ Á¦°øÇØ ÁÖ´Â °É±î¿ä?  this´Â ´ÙÀ½°ú °°Àº ¸î °¡Áö Áß¿äÇÑ Æ¯Â¡À» °®½À´Ï´Ù.

1. °´Ã¼ Àڽſ¡ ´ëÇÑ ÂüÁ¶°ªÀ» °®½À´Ï´Ù.
2. ¸Þ¼Òµå ³»¿¡¼­¸¸ »ç¿ëµË´Ï´Ù.
3. ¸Å°³º¯¼ö¿Í °´Ã¼ ÀÚ½ÅÀÌ °¡Áö°í ÀÖ´Â º¯¼öÀÇ À̸§ÀÌ °°À» °æ¿ì À̸¦ ±¸ºÐÇϱâ À§ÇØ ÀÚ½ÅÀÌ °¡Áö°í ÀÖ´Â º¯¼ö ¾Õ¿¡ this¸¦ »ç¿ëÇÕ´Ï´Ù.
4. °´Ã¼ »ý¼ºÀÚ ³»¿¡¼­ ´Ù¸¥ »ý¼ºÀÚ¸¦ È£ÃâÇϱâ À§ÇØ »ç¿ëÇÕ´Ï´Ù.
5. °´Ã¼ Àڽſ¡ ´ëÇÑ ÂüÁ¶°ªÀ» ¸Þ¼Òµå¿¡ Àü´ÞÇϰųª ¸®ÅÏÇØ ÁÖ±â À§ÇØ »ç¿ëÇϱ⵵ ÇÕ´Ï´Ù.
6. this¸¦ »ç¿ëÇÔÀ¸·Î½á, ¸ðÈ£ÇÏÁö ¾Ê°í Á»´õ ¸íÈ®ÇÑ ÇÁ·Î±×·¥À» ÀÛ¼ºÇÒ ¼ö ÀÖ½À´Ï´Ù.

»ç¿ëÇÒ ¶§ÀÇ ¿¹


this

this.¸â¹öº¯¼ö

this(¸Å°³º¯¼ö);

 

this¿¹Á¦1
 

public class ThisTest2
{
         private int
number;
         private String name;

         public ThisTest2(String name, int number)
         {
                  this
.name = name;
                  this
.number = number;         
         }
         public void
print()
         {
                  System.out.println("name:" + this.name + " number:" + this
.number);                  
                  //System.out.println("name:" + name + " number:" + number);          
         }
         public void
myprint()
         {
                  System.out.println("This Å×½ºÆ®"
);
                  this
.print();
                 
//print();
         }
         public static void
main(String[] args)
         {
                  ThisTest2 th = new ThisTest2("È«±æµ¿", 20
);
                  th.print();
                 
th.myprint();                   
         }
}

 °á°úÃâ·Â E:\JavaSource>java ThisTest2
name:È«±æµ¿ number:20
name:È«±æµ¿ number:20
 

ÀÌ ÇÁ·Î±×·¥¿¡¼­´Â ¸â¹öº¯¼ö·Î µÎ°³ÀÇ ¸â¹ö¸¦ °¡Áö°í ÀÖ´Ù. ÀÌ ¸â¹ö¸¦ Áö½ÃÇÏ´Â ¹æ¹ýÀº Ŭ·¡½º³»¿¡¼­´Â ÀϹÝÀûÀ¸·Î private String name°ú private int number¸¦ ¸¶À½´ë·Î »ç¿ëÇÒ ¼ö ÀÖ´Ù. ÇÏÁö¸¸ »ý¼ºÀÚ ÇÔ¼ö¸¦ º»´Ù¸é nameÀÌ Áߺ¹µÇ¾î ÀÖ´Ù. À̰ÍÀº ÇÔ¼öÀÇ ¸Å°³º¯¼ö¿Í Ŭ·¡½ºÀÇ ¸â¹öº¯¼ö°¡ µ¿ÀÏÇÑ À̸§À» °¡Áö°í ÀÖ´Ù. À̰ÍÀ» ±¸ºÐÇϱâ À§ÇÑ ¹æ¹ýÀ¸·Î this¸¦ »ç¿ëÇϰí ÀÖ´Ù.

      public ThisTest2(String name, int number)
         {
                 
this.name = name;
                 
this.number = number;         
         }

ÀÌ·¯ÇÑ »óȲ¿¡¼­´Â this¸¦ ºÙÀÌ¸é ¸â¹ö º¯¼ö¸¦ °¡¸£Å°´Â °ÍÀÌ¸ç ±×³É nameÀ̳ª number¶ó´Â º¯¼ö¸¦ »ç¿ëÇÒ °æ¿ì´Â °¡±î¿î °÷ÀÇ º¯¼ö¼±¾ð¿¡ ¿µÇâÀ» ¹ÌÄ¡°Ô µÈ´Ù. ±×·¸±â ¶§¹®¿¡ name°ú number´Â ÇÔ¼öÀÇ ¸Å°³º¯¼ö¸¦ ¸»ÇÏ´Â °ÍÀ̸ç this.name°ú this.numberÀº ¸â¹öº¯¼ö¸¦ °¡¸£Å°´Â °ÍÀÌ´Ù.

this¶ó´Â Ű¿öµå´Â ¸â¹ö¶ó´Â °ÍÀ» ¾Ë¸®´Â ÇϳªÀÇ Ç¥½Ã¹æ¹ýÀ̳ª ¸¸¾à À§ÀÇ »óȲó·³ Ư¼öÇÑ °æ¿ì°¡ ¾Æ´Ï¸é thisŰ¿öµå¸¦ »ç¿ëÇÏÁö ¾Ê¾Æµµ ¹«¹æÇÏ´Ù. ÇÏÁö¸¸ thisŰ¿öµå¸¦ »ç¿ëÇÏ¿© ¾î´À ¼Ò¼ÓÀÇ ¸Þ¼­µåÀÎÁö º¯¼öÀÎÁö¸¦ ¾Ë¸®´Â Ç¥½ÃÀ̱⵵ Çϸç ÀÚ½ÅÀ» °¡¸£Å°´Â À¯ÀÏÇÑ ¹æ¹ýÀ̱⵵ ÇÏ´Ù.

 

 2 this ¿¹Á¦2
 

//this´Â ÇöÀç °´Ã¼¸¦ À§ÇÑ ÇÚµéÀÌ´Ù!

public class ThisTest
{
    private int i = 0
;
    public
ThisTest plusCount() 
   
       i++; 
       return this

    }

    public void print() 
   
        System.out.println("member i = "
+ i); 
    }
    public static void
main(String args[]) 
    {
       ThisTest tt = new ThisTest();
        ThisTest tt2 = tt.plusCount();
        tt2.plusCount();
        tt2.print();
   
}
}

 °á°úÃâ·Â

E:\JavaSource>java ThisTest
member i = 2

  Ŭ·¡½º´Â ÀÚ½ÅÀÇ ÇÚµéÀ» ¸®ÅÏÇϱâ À§Çؼ­ this¸¦ »ç¿ëÇÑ´Ù. ¸ðµç °÷¿¡¼­ this´Â À¯¿ëÇÑ ¸éÀ» ¹ßÈÖÇÑ´Ù. ÀÌ °æ¿ì´Â ÀÚ½ÅÀ» ÇÚµéÀ» ´Ù¸¥ °÷¿¡ ÁÖ°í ½Í¾îµµ ÁÙ ¼ö°¡ ¾ø´Ù. Áï ÀÚ½ÅÀ» °¡¸£Å³ ¼ö ÀÖ´Â ¹æ¹ýÀÌ thisÀ̿ܿ¡´Â ¾ø±â¶§¹®ÀÌ´Ù.

 





 


Copyright ¨Ï 1999-2000 Schatzi. All rights reserved.
http://www.jabook.org
http://schatzi.konkuk.ac.kr