생성자의 초기화 및 전체적인 개념 설명은
❗ JAVA 시리즈 11번 ❗
바로 여기에 설명해두었으니, 참고해보자!
클래스 하나, 생성자 여러개
하나의 클래스에 이름이 같은 생성자를 여러 개 선언
❗ 생성자의 매개변수 개수와 타입을 다르게 ❗
public Student() {}
main
)에서 UserInfo userLee = new UserInfo();
userLee.userId = "a12345";
userLee.userPassWord = "zxcvbn12345";
userLee.userName = "Lee";
userLee.phoneNumber = "01034556699";
userLee.userAddress = "Seoul, Korea";
System.out.println(userLee.showUserInfo());
UserInfo userKim = new UserInfo("b12345", "09876mnbvc", "Kim");
System.out.println(userKim.showUserInfo());
public Student(String stdName, int stdScore, boolean stdIsPassed,
String stdMajor) {
name = stdName;
score = stdScore;
isPassed = stdIsPassed;
major = stdMajor;
}
this
사용public Student(String n, int s, boolean e, String m) { // 너무축약
name = n; // 이름
score = s; // 점수
isPassed = e; // 시험 패스여부
major = m; // 전공
}
public Student(String studentName, int studentScore, boolean studentIsPassed, String studentMajor) {
// 너무 김
name = studentName;
score = studentScore;
isPassed = studentExamPassd;
major = studentMajor;
}
public Student(String name, int score, boolean isPassed, String major) {
name = name; // 완전 똑같음 -> 이럴땐 this 쓰기
score = score;
isPassed = isPassed;
major = major;
}
this.
사용하기this.변수
: 자동으로 멤버변수를 가리킴public Student(String name, int score, boolean isPassed, String major) { // 매개변수
this.name = name;
this.score = score;
this.isPassed = isPassed;
this.major = major;
}
this()
생성자객체 자신을 가리키는 것
클래스에 생성자가 여러 개 인경우
→this를 이용하여 생성자에서 다른 생성자를 호출할 수 있음
생성자에서 다른 생성자를 호출하는 경우
→인스턴스의 생성이 완전하지 않은 상태이므로
this() statement
이전에 다른 statement를 쓸 수 없음
this()
생성자this.멤버변수
→ 해당 클래스의 멤버변수매개변수
→ 현재 메서드의 매개변수this.멤버변수 = 매개변수;
this
→ 첫번째 메서드의 매개변수 가리킴(매개변수, 매개변수)
→ 현재(두번째) 메서드의 매개변수를 가리킴this(매개변수, 매개변수)
JAVA 시리즈 11번 중 생성자를 정리했던 것보다
조금 더 추가할 개념들을 정리해보았다..!