λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
이둠/Study

μƒμ„±μž Constructor

by 6161990 2021. 7. 19.

πŸ“Œ μƒμ„±μž Constructor

μΈμŠ€ν„΄μŠ€κ°€ 생성될 λ•Œ ν˜ΈμΆœλ˜λŠ” 'μΈμŠ€ν„΄μŠ€ μ΄ˆκΈ°ν™” λ©”μ„œλ“œ', μΈμŠ€ν„΄μŠ€ λ³€μˆ˜μ˜ μ΄ˆκΈ°ν™” μž‘μ—…μ— μ‚¬μš©λ˜λ©°, μΈμŠ€ν„΄μŠ€ 생성 μ‹œμ— μ‹€ν–‰λ˜μ–΄μ•Ό ν•  μž‘μ—…μ„ μœ„ν•΄μ„œλ„ μ‚¬μš©λœλ‹€.

 

 

πŸ“ μƒμ„±μžμ˜ 쑰건 및 νŠΉμ§•

  • 일반적으둜 μ–΄λ””μ„œλ“  ν΄λž˜μŠ€μ— λŒ€ν•œ 객체 생성이 κ°€λŠ₯ν•˜λ„λ‘ 'public'으둜 지정.
  • 객체λ₯Ό 생성할 λ•Œ new ν‚€μ›Œλ“œμ™€ ν•¨κ»˜ λ…ΈμΆœ (객체 생성 μ™Έμ—λŠ” ν˜ΈμΆœν•  수 μ—†μŒ)
  • μΈμŠ€ν„΄μŠ€λ₯Ό μ΄ˆκΈ°ν™” ν•˜λŠ” μ½”λ“œκ°€ κ΅¬ν˜„λ¨ (주둜 멀버 λ³€μˆ˜ μ΄ˆκΈ°ν™”)
  • λ°˜ν™˜ 값이 μ—†μŒ(μ΄ˆκΈ°ν™”λ₯Ό λͺ©μ μœΌλ‘œ ν•˜κΈ° λ•Œλ¬Έμ—), μƒμ†λ˜μ§€ μ•ŠμŒ.
  • μƒμ„±μžλŠ” 클래슀 이름과 동일.


πŸ“ μƒμ„±μž 생성 κ³Όμ •λ¨Όμ € μ—°μ‚°μž new에 μ˜ν•΄μ„œ λ©”λͺ¨λ¦¬(heap)에 ν•΄λ‹Ή 클래슀의 μΈμŠ€ν„΄μŠ€κ°€ μƒμ„±λœλ‹€. κ·Έ ν›„, μƒμ„±μžκ°€ ν˜ΈμΆœλ˜μ–΄ μˆ˜ν–‰λ˜κ³  μ—°μ‚°μž new의 결과둜 μƒμ„±λœ μΈμŠ€ν„΄μŠ€μ˜ μ£Όμ†Œκ°€ λ°˜ν™˜λ˜μ–΄ μ°Έμ‘°λ³€μˆ˜μ— μ €μž₯λœλ‹€.
πŸ“ this와 this()

  • this : 호좜 μ‹œ 클래슀 이름 λŒ€μ‹  μ‚¬μš©ν•˜λŠ” μ˜ˆμ•½μ–΄, μΈμŠ€ν„΄μŠ€ μžμ‹ μ„ κ°€λ¦¬ν‚€λŠ” κ·Έ μ£Όμ†Œκ°€ μ €μž₯λ˜μ–΄μžˆλŠ” μ°Έμ‘°λ³€μˆ˜λ₯Ό μ§€μΉ­ν•˜λ©° λͺ¨λ“  μΈμŠ€ν„΄μŠ€ λ©”μ„œλ“œμ—μ„œ μˆ¨κ²¨μ§„ μ±„λ‘œ μ‘΄μž¬ν•œλ‹€.
  • this() : 같은 클래슀의 λ‹€λ₯Έ μƒμ„±μžλ₯Ό ν˜ΈμΆœν•  λ•Œ μ‚¬μš©ν•œλ‹€.


πŸ“ κΈ°λ³Έμƒμ„±μž(default constructor)

  • ν•΄λ‹Ή 클래슀λ₯Ό newν•  λ•Œ μ‚¬μš©ν•  κΈ°λ³Έ μƒμ„±μžλ₯Ό λœ»ν•¨.
  • κΈ°λ³Έ μƒμ„±μžμ˜ λ©€λ²„λ³€μˆ˜λŠ” μƒμ„±μžκ°€ νž™ λ©”λͺ¨λ¦¬μ— 할당이 될 λ•Œ, JVM이 κΈ°λ³Έκ°’μœΌλ‘œ κΈ°λ³Έ μƒμ„±μžμ˜ λ©€λ²„λ³€μˆ˜λ₯Ό μ΄ˆκΈ°ν™”ν•¨.
  • ν•˜λ‚˜μ˜ ν΄λž˜μŠ€μ—λŠ” λ°˜λ“œμ‹œ ν•˜λ‚˜ μ΄μƒμ˜ μƒμ„±μžκ°€ μ‘΄μž¬ν•΄μ•Όν•¨.
  • ν”„λ‘œκ·Έλž˜λ¨Έκ°€ μƒμ„±μžλ₯Ό κ΅¬ν˜„ν•˜μ§€ μ•ŠμœΌλ©΄ μ»΄νŒŒμΌλŸ¬κ°€ μƒμ„±μž μ½”λ“œλ₯Ό λ„£μ–΄μ€Œ. => κΈ°λ³Έ μƒμ„±μž
  • κΈ°λ³Έ μƒμ„±μžλŠ” 맀개 λ³€μˆ˜κ°€ μ—†κ³ , κ΅¬ν˜„λΆ€κ°€ μ—†μŒ

κΈ°λ³Έ μƒμ„±μž

public class Student {

	public int studentID; 
    public String studentName; 
    public String address; 
    
    public Student(){ //이 μ•ˆμ—μ„œλŠ” Student의 μΈμŠ€ν„΄μŠ€ λ³€μˆ˜κ°€ λ‚΄λΆ€μ μœΌλ‘œ μ΄ˆκΈ°κ°’μœΌλ‘œ κ΅¬ν˜„λœλ‹€. 
    (intλŠ” 0, String은 null κ°’ .... λ“±μœΌλ‘œ! } 
    
 }

ν΄λž˜μŠ€μ— λ‹€λ₯Έ μƒμ„±μžκ°€ μžˆλŠ” 경우 λ””ν΄νŠΈ μƒμ„±μžλŠ” μ œκ³΅λ˜μ§€ μ•ŠμŒ

public class Student { 
	public int studentID; // μ—¬κΈ° λ³€μˆ˜λ“€μ€ μΈμŠ€ν„΄μŠ€ μƒμ„±μ‹œ, νž™ λ©”λͺ¨λ¦¬μ— ν• λ‹Ήν•  λ•Œ μžλ™ μ΄ˆκΈ°ν™”λœλ‹€ 
    public String studentName; // 보톡 int = 0 , String = null 둜. 
    public String address; // 전달받은 값이 μžˆλ‹€λ©΄ κ·Έ κ°’μœΌλ‘œ. 
    
    // public Student(){} 
    
    public Student(int id, String name){ 
   	 studentID = id; 
   	 studentName = name; 
    } 
 }

λ‹€λ₯Έ μƒμ„±μžλ₯Ό λ§Œλ“€κ²½μš° mainμ—μ„œμ˜ 였λ₯˜

public class StudentTest { 
	public static void main(String[] args) { 
    	Student studentLee = new Student(); // 였λ₯˜μ§€μ  
        studentLee.studentName = "Jenny"; 
        studnetLee.address = "ν•œλ‚¨λ™"; 
    } 
 }

κ΅¬ν˜„ν•  λ•Œ λ°˜λ“œμ‹œ ν•„μš”ν•œ λ©”μ„œλ“œκ°€ ν•„μš”ν•œ 경우("이름은 λ°˜λ“œμ‹œ 맀번 κ΅¬ν˜„μ„ ν• κ±°μ•Ό")

public class Student { 
	public int studentID; 
    public String studentName; 
    public String address; 
    
    public Student(String name){ 
    	studentName = name; 
    } 
    
    public static void main(String[] args) { 
   	 	Student studentLee = new Student(Jenny); //studentLee.studentName = "Jenny"; ν•„μš”μ—†μŒ. ν•„μˆ˜ λ©”μ†Œλ“œλ‘œ κ΅¬ν˜„ν•΄λ†“μ•˜κΈ°λ•Œλ¬Έ. 
    	studnetLee.address = "ν•œλ‚¨λ™";
    } 
    
  }

πŸ“Œ μƒμ„±μž μ˜€λ²„λ‘œλ”© Constructor Overroding

ν΄λž˜μŠ€μ— λŒ€ν•œ 객체λ₯Ό new 생성할 λ•Œ, μ΄ˆκΈ°κ°’μ„ μ£ΌλŠ” 방법을 λ‹€μ–‘ν™”μ‹œν‚€κ³  싢을 λ•Œ μ‚¬μš©ν•œλ‹€.μ˜€λ²„λ‘œλ”©μ˜ λͺ©μ  : λ‹€μ–‘ν•œ κ°’μ˜ μ²˜λ¦¬λ°©μ‹μ„ ν•˜λ‚˜μ˜ μ΄λ¦„μœΌλ‘œ λ‹€λ£¨κ³ μž ν•  λ•Œ.

  • μƒμ„±μžλ₯Ό 두 개 이상 κ΅¬ν˜„ν•˜λŠ” 경우
  • κΈ°λ³Έ μƒμ„±μž, λ§€κ°œλ³€μˆ˜ μžˆλŠ” μƒμ„±μž 쀑볡일 λ•Œ Overriding ν•œκ²ƒμž„.=> λ§€κ°œλ³€μˆ˜κ°€ λ°˜λ“œμ‹œ λ‹¬λΌμ•Όν•œλ‹€. λ§€κ°œλ³€μˆ˜μ˜ 갯수, μžλ£Œν˜•μ˜ μˆœμ„œμ— 따라 μ—¬λŸ¬κ°œμ˜ μƒμ„±μžλ₯Ό λ§Œλ“€ μˆ˜μžˆλ‹€.
  • μ‚¬μš©ν•˜λŠ” μ½”λ“œμ—μ„œ μ—¬λŸ¬ μƒμ„±μž 쀑 μ„ νƒν•˜μ—¬ μ‚¬μš©ν•  수 있음.
  • private λ³€μˆ˜λ„ μƒμ„±μžλ₯Ό μ΄μš©ν•˜μ—¬ μ΄ˆκΈ°ν™”λ₯Ό ν•  μˆ˜λ„ 있음(객체 μ΄ˆκΈ°ν™” μž‘μ—…μ„ ν•  λ•Œ μƒμ„±μžλ₯Ό κ°€μ Έλ‹€ 쓰기도 함).
μƒμ„±μž μ˜€λ²„λ‘œλ”© Constructor Overroding

public class Student { 
public int studentID; 
public String studentName; 
public String address;

public Student(String name){ 
	studentName = name; 
} 

public Student(int id, String name, String address){ 
    studentID = id; 
    studentName = name; 
    this.address = address; 
} 

public void showStudentInfo(){ 
System.out.println(studentName+ "," +address); 
} 

public String getStudentName(){ 
return studentName; 
} 

public static void main(String[] args) { 
    Student studentKim = new Student("Jenny"); //studentKim.studentName = "Jenny"; ν•„μˆ˜λ©”μ†Œλ“œλ‘œ κ΅¬ν˜„ 
    studentKim.address = "ν•œλ‚¨λ™"; 
    studentKim.showStudentInfo(); 
    Student studentBlack = new Student(100,"λΈ”λž™ν•‘ν¬","합정동"); //studentBlack.studentName = "λΈ”λž™ν•‘ν¬"; ν•„μˆ˜λ©”μ†Œλ“œλ‘œ κ΅¬ν˜„ 
    //studentBlack.address = "합정동"; ν•„μˆ˜λ©”μ†Œλ“œλ‘œ κ΅¬ν˜„ 
    studentBlack.showStudentInfo();
  } 
}