3.4 Constructor (»ý¼ºÀÚ)

ÀÛ¼ºÀÚ : ÁøÀº¿µ ( 2004-07-26 )

[¸ÞÀÎ] [¸ñ·Ï]

¸ñÂ÷

3.4.1 Construction À̶õ

new Å°¿öµå¸¦ ÅëÇØ °´Ã¼¸¦ »ý¼ºÇÏ´Â ¿ªÇÒÀ» ÇÑ´Ù. »ý¼ºÀÚ È£ÃâÇÏ´Â °úÁ¤Àº ¾Æ·¡¿Í °°´Ù.

ÇϳªÀÇ Å¬·¡½º¿¡´Â »ý¼ºÀÚ°¡ 0°³ ÀÌ»ó ÀÖÀ» ¼ö ÀÖÀ¸¸ç, °³¹ßÀÚ°¡ »ý¼ºÀÚ¸¦ ±¸ÇöÇÏÁö ¾ÊÀ¸¸é ÀÚ¹Ù ÄÄÆÄÀÏ·¯°¡ ÄÄÆÄÀϽà ÀÚµ¿À¸·Î default Constructor ¸¦ »ý¼ºÇØÁØ´Ù.

»ý¼ºÀÚÀÇ Çü½ÄÀº ¾Æ·¡¿Í °°´Ù.

Áß¿äÇÑ °ÍÀº ¸®ÅÏŸÀÔÀÌ ¾ø´Ù´Â °ÍÀÌ´Ù.
class Car{
        String name ; 

        public Car( String n ){
                name = n ; 
        }
}


3.4.2 Constructor »ý¼ºÇÏ·Á¸é

  • Á¢±ÙÁ¦ÇÑÀÚ´Â ¿ÜºÎ¿¡¼­µµ °´Ã¼¸¦ »ý¼ºÇÏ°Ô ÇØ¾ß Çϱ⠶§¹®¿¡ publicÀ¸·Î ÁöÁ¤ÇÑ´Ù.
  • »ý¼ºÀÚ ¸íÀº ¹Ýµå½Ã Ŭ·¡½º¸í°ú µ¿ÀÏÇÏ°Ô ÇÑ´Ù.
  • ¸Å°³º¯¼ö´Â ¸â¹öº¯¼ö¿¡ °ªÀ» ´ëÀÔÇÏ°í ½ÍÀº º¯¼öÀÇ Å¸ÀÔÀ» ÁöÁ¤ÇÑ´Ù.
class Car {
	//¸â¹öº¯¼ö ¼±¾ð
	String name ; 
	String brand ;
	String color ; 

	public Car(){}  //default constructor

	pbulic Car( String n ){ //name ¸â¹öº¯¼ö¿¡ nÀÇ °ªÀ» ´ëÀÔÇÏ´Â »ý¼ºÀÚ
			name = n ; 
	}
	
	public Car( String n , String b , String c ){   //¸â¹öº¯¼ö¿¡ °¢°¢ÀÇ °ªÀ» ´ëÀÔÇÏ´Â »ý¼ºÀÚ
			name = n ; 
			brand = b ;
			color = c ; 
	}
}

ÇϳªÀÇ Å¬·¡½º¿¡ »ý¼ºÀÚ°¡ ²À Çϳª¸¸ ÀÖÀ» ÇÊ¿ä´Â ¾ø´Ù. Ãß»óÈ­ ÇÒ ¶§ ÇÊ¿äÇÑ °¹¼ö¸¸Å­ »ý¼ºÀÚ¸¦ ±¸ÇöÇÒ ¼ö ÀÖ´Ù.

±×·¸´Ù¸é »ý¼ºÀÚ¸¦ ±âÀÔÇÏ´Â °Í°ú ±×·¸Áö ¾ÊÀº°ÍÀÇ Â÷ÀÌÁ¡Àº ¹«¾ùÀϱî?
3-3¿¡ ÀÖ¾ú´ø Employees Ŭ·¡½ºÀÇ °´Ã¼¸¦ »ý¼ºÇØ º¸ÀÚ
public class EmployeesTest{
        public static void main( String [] args ){
                Employees e1 = new Employees() ; 
                e1.setEmpID( "emp001" ) ; 
                e1.setName( "È«±æµ¿" ) ; 
        }
}

°´Ã¼¸¦ »ý¼ºÇÑ ÈÄ ¸â¹öº¯¼ö¿¡ Á÷Á¢ °ªÀ» ´ëÀÔÇÒ ¼ö´Â ¾ø´Ù. °¢ ¸â¹öº¯¼ö¿¡´Â º¯¼öÀÇ °ªÀ» º¸È£Çϱâ À§ÇØ Á¢±ÙÁ¦ÇÑÀÚ¸¦ privateÀ¸·Î ÁÖ¾ú±â ¶§¹®ÀÌ´Ù. Á¢±ÙÀÌ °¡´ÉÇÑ ¸Þ¼Òµå¸¦ ÅëÇØ °ªÀ» ´ëÀÔÇØ¾ß Çϴµ¥, ÇϳªÇϳª ´ëÀÔÇÏ·Á´Ï ±ÍÂú´Ù. ¾Æ·¡ÀÇ ·ÎÁ÷À¸·Î ¹Ù²ãº¸ÀÚ.

public class Employees {
        
	//¸â¹öº¯¼ö ¼±¾ð
	private String empID ; 
	private String name ; 
	private String position ; 
	private int salary ; 
	
	//»ý¼ºÀÚ ¼±¾ð 
	public Employees ( String id , String n ){
	   empID = id ; 
	   name = n ; 
	}

	//¸Þ¼Òµå ¼±¾ð
	//public void print(){}

	// public int calSalary(){}

	// set Method
	.......... »ý·« ............	   
}
À§¿Í °°ÀÌ ÀÛ¼ºÇÑ´Ù¸é ¾Æ·¡¿Í °°ÀÌ °´Ã¼¸¦ »ý¼ºÇÒ ¼ö ÀÖ´Ù.
public class EmployeesTest{
        public static void main( String [] args ){
                Employees e1 = new Employees( "emp001" , "È«±æµ¿" ) ; 
        }
}

¿©±â¿¡¼­ ÁÖÀÇÇÒ °ÍÀº °³¹ßÀÚ°¡ Çϳª ÀÌ»óÀÇ »ý¼ºÀÚ¸¦ ±¸ÇöÇÑ´Ù¸é ÄÄÆÄÀÏ·¯´Â default constructor¸¦ ¸¸µé¾î ÁÖÁö ¾Ê´Â´Ù´Â °ÍÀÌ´Ù. Áï,
public class EmployeesTest{
	public static void main( String [] args ){
		Employees e1 = new Employees( "emp001" , "È«±æµ¿" ) ; 
		Employees e2 = new Employees() ; 
	}
}
ÄÄÆÄÀÏ

---------- javac ----------
EmployeesTest.java:3: cannot resolve symbol
symbol : constructor Employees (java.lang.String,java.lang.String)
location: class Employees
Employees e1 = new Employees( "emp001" , "È«±æµ¿" ) ;
^ 1 error
Ãâ·Â ¿Ï·á (2ÃÊ °æ°ú) - Á¤»ó Á¾·á
Áï Employess() ¸¦ ãÀ»¼ö ¾ø´Ù°í ³ª¿Â´Ù. ÀÚÁÖ Âø°¢ÇÏ´Â ¹®Á¦À̱⠶§¹®¿¡ ¹Ýµå½Ã ¤°í ³Ñ¾î°¡¾ßÇÑ´Ù