#55_140829_JAVA_THIS
# THIS
l This 키워드
1. 클래스 멤버 메소드의 매개변수와 클래스 멤버 변수의 이름이 중복되는 경우
2. this 키워드를 이용하면 생성자 내에서 다른 생성자를 호출할 수 있다
This 예제 |
class Person { private int perID; private int milID; private int birthYear; private int birthMonth; private int birthDay;
public Person(int perID, int milID, int bYear, int bMonth, int bDay) { this.perID=perID; this.milID=milID; birthYear=bYear; birthMonth=bMonth; birthDay=bDay; } public Person(int pID, int bYear, int bMonth, int bDay) { this(pID, 0, bYear, bMonth, bDay); // milID 가 없어서 0 을 할당해주고있다. } public void showInfo() { System.out.println("민번: "+perID); System.out.println("생년월일: "+birthYear+"/"+birthMonth+"/"+birthDay); if(milID!=0) System.out.println("군번: "+milID+'\n'); else System.out.println("군과 관계 없음 \n"); } } |