관리 메뉴

βœπŸ»κΈ°λ‘ν•˜λŠ” λΈ”λ‘œκ·Έ

[ JAVA ] μƒμ„±μžμ™€ μƒμ„±μž μ˜€λ²„λ‘œλ”© λ³Έλ¬Έ

Language/Java

[ JAVA ] μƒμ„±μžμ™€ μƒμ„±μž μ˜€λ²„λ‘œλ”©

μ©μ‹œν‚΄ 2020. 9. 4. 19:22
728x90

μƒμ„±μžλž€?

  • 객체λ₯Ό 생성할 λ•Œ new ν‚€μ›Œλ“œμ™€ ν•¨κ»˜ 호좜, 객체 생성 μ΄μ™Έμ—λŠ” ν˜ΈμΆœν•  수 μ—†μŒ
  • λ°˜ν™˜ 값이 μ—†μŒ, μƒμ†λ˜μ§€ μ•ŠμŒ
  • μƒμ„±μžλŠ” 클래슀의 이름과 동일
  • μΈμŠ€ν„΄μŠ€λ₯Ό μ΄ˆκΈ°ν™”ν•˜λŠ” 역할은 함 ( 멀버 λ³€μˆ˜ μ΄ˆκΈ°ν™” )
  • -> 클래슀λ₯Ό μƒμ„±ν• λ•Œλ§ˆλ‹€ λ‚΄κ°€ μ›ν•˜λŠ” κ°’μœΌλ‘œ μ΄ˆκΈ°ν™”ν•˜κ³  싢을 λ•ŒλΌκ³  μƒκ°ν•˜μž, 이 클래슀λ₯Ό λ§Œλ“€ λ•ŒλŠ” 이 값듀을 λ°›μ•„μ„œ κ΅¬ν˜„ν•˜κ² λ‹€.
    public int studentId;
    public String studentName;
    public String address;

	//μƒμ„±μž κ΅¬ν˜„λΆ€
    public Student (int id, String name){
		studentId = id;
		studentName = name;
	    //μ΄ˆκΈ°ν™” ν•˜μ§€ μ•Šμ€ addressλ³€μˆ˜μ—λŠ” null듀어감
    }

 

κΈ°λ³Έ μƒμ„±μž ( Default Constructor )

  • ν•˜λ‚˜μ˜ ν΄λž˜μŠ€μ—λŠ” λ°˜λ“œμ‹œ ν•˜λ‚˜ μ΄μƒμ˜ μƒμ„±μžκ°€ μ‘΄μž¬ν•΄μ•Ό 함
  • ν”„λ‘œκ·Έλž˜λ¨Έκ°€ μƒμ„±μžλ₯Ό κ΅¬ν˜„ν•˜μ§€ μ•ŠμœΌλ©΄ μ»΄νŒŒμΌλŸ¬κ°€ μƒμ„±μž μ½”λ“œλ₯Ό λ„£μ–΄μ€Œ ⇒ κΈ°λ³Έ μƒμ„±μž
  • κΈ°λ³Έ μƒμ„±μžλŠ” 맀개 λ³€μˆ˜κ°€ μ—†κ³ , κ΅¬ν˜„λΆ€κ°€ μ—†λ‹€.
  • λ§Œμ•½ ν΄λž˜μŠ€μ— λ‹€λ₯Έ μƒμ„±μžκ°€ 있으면 λ””ν΄νŠΈ μƒμ„±μž κ΅¬ν˜„ν•˜μ§€ μ•ŠμŒ.
public Student(){
	//κΈ°λ³Έ μƒμ„±μž
}

 

 

μƒμ„±μž μ˜€λ²„ λ‘œλ”© ( Constructor Overloading )

 

μƒμ„±μž μ˜€λ²„ λ‘œλ”©μ΄λž€ ν•œ 클래슀 μ•ˆμ— 같은 μ΄λ¦„μ΄μ§€λ§Œ λ°›λŠ” λ§€κ°œλ³€μˆ˜κ°€ λ‹€λ₯Έ 것이 μ—¬λŸ¬ 개 μ‘΄μž¬ν•˜λŠ” 경우λ₯Ό λœ»ν•¨

μœ„μ—μ„œ ν•˜λ‚˜μ˜ ν΄λž˜μŠ€μ—λŠ” λ°˜λ“œμ‹œ ν•˜λ‚˜ μ΄μƒμ˜ μƒμ„±μžκ°€ μ‘΄μž¬ν•΄μ•Ό ν•œλ‹€κ³  ν–ˆλ‹€.

 

package constructor;

public class Student {

    public int studentId;
    public String studentName;
    public String address;


    public Student (int id, String name){
        studentId = id;
        studentName = name;
    }

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

    public String getStudentName(){
        return studentName;
    }

}

ν•˜μ§€λ§Œ 메인 ν•¨μˆ˜μ—μ„œ 싀행을 해보면 Student studentLee = new Student(); λ₯Ό 톡해 객체λ₯Ό μƒμ„±ν•˜λ©΄

μ•„λž˜μ™€ 같이 Student(int , String) ν˜• μƒμ„±μžλ₯Ό μ΄μš©ν•΄μ„œ 객체λ₯Ό 생성할 수 μ—†λ‹€κ³  ν•œλ‹€.

μœ„μ˜ μ½”λ“œλŠ” μƒμ„±μžλ₯Ό (int, string) νƒ€μž…μœΌλ‘œ λ§Œλ“ λ‹€κ³  μ •μ˜ν–ˆκΈ° λ•Œλ¬Έμ΄λ‹€.

 

 

Student(int, string) ν˜•μœΌλ‘œ 객체λ₯Ό μƒμ„±ν•˜κ±°λ‚˜ μ•„λž˜ λ°©μ‹μ²˜λŸΌ μƒμ„±μž μ˜€λ²„ λ‘œλ”©μœΌλ‘œ 객체λ₯Ό 생성해야 ν•œλ‹€.

μ»΄νŒŒμΌλŸ¬λŠ” ν΄λž˜μŠ€μ— ν•˜λ‚˜λΌλ„ μƒμ„±μžκ°€ 있으면 λ””ν΄νŠΈ μƒμ„±μžλ₯Ό μ œκ³΅ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— ν”„λ‘œκ·Έλž˜λ¨Έκ°€ 직접 λ„£μ–΄μ€€λ‹€.

 

 

package constructor;

public class Student {

    public int studentId;
    public String studentName;
    public String address;

    public Student(){
        //κΈ°λ³Έ μƒμ„±μž μΆ”κ°€
    }

    public Student (int id, String name){
        studentId = id;
        studentName = name;
    }

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

    public String getStudentName(){
        return studentName;
    }

}

 

κΈ°λ³Έ μƒμ„±μžμ™€ ν”„λ‘œκ·Έλž˜λ¨Έκ°€ μ›ν•˜λŠ” ν˜•μ‹μ˜ μƒμ„±μžλ₯Ό 생성 ν›„ Student() 객체 생성이 μ •μƒμ μœΌλ‘œ μ΄λ£¨μ–΄μ§€λŠ” 것을 확인할 수 μžˆλ‹€.


μ°Έκ³ )

패슀트 캠퍼슀 - Java μ›Ή 개발 λ§ˆμŠ€ν„° μ˜¬μΈμ› νŒ¨ν‚€μ§€ Online. / 객체지ν–₯ ν”„λ‘œκ·Έλž˜λ° 박은쒅 κ°•μ‚¬λ‹˜

728x90
λ°˜μ‘ν˜•