private 접근제어지시자로 접근이 제한된 멤버변수에 접근하라면
getter, setter 메서드를 쓸 수 있어야 한다.
this 키워드를 활용하면 이 과정을 포함해 클래스 설계를 쉽게 할 수 있다.
this 키워드의 역할은
1.자신의 주소를 나타낸다.
2.생성자에서 또 다른 생성자를 호출
3.자신의 주소값을 반환한다.
package com.this1;
//Person 클래스 설계
public class Person {
/*
this의 3가지 사용법
1. 자기 자신의 주소를 가리킨다.
2. 생성자에서 다른 생성자를 호출한다.
3. 자신의 주소값을 반환시킨다.
먼저 1번 용법을 알아본다 - 문법, 이유
*/
//속성
private String name;
private int age;
private String phone;
private String gender;
//생성자
public Person(String name, int age) {
// 멤버변수 = 매개변수
this.name = name; // this를 사용했다
this.age = age; // 변수이름 떠올리는 수고를 줄여준다.
System.out.println("1번 생성자 호출됨");
}
/*
2번째 사용 방법
this();
*/
public Person(String name, int age, String phone) {
//this.name = name;
//this.age = age;
this(name, age); //다른 생성자 구분, 앞에 뭐가 있으면 안됨
this.phone = phone;
System.out.println("2번 생성자 호출됨");
}
public Person(String name, int age, String phone, String gender) {
// this.name = name;
// this.age = age;
// this.phone = phone;
this(name, age, phone);
this.gender = gender;
System.out.println("3번 생성자 호출됨");
}
/*
3번째 사용 방법
현재 생성된 객체
즉, 나의 주소를 외부로 반환한다.
디자인 패턴
프로그램을 작성할 때
일괄된 문제가 발생할 수 있다.
이 때 특정 코드의 패턴을 만들어서
해결해 가는 방법이 있다.
*/
public Person getPerson() {
return this;
}
} // end of class
package com.this1;
public class PersonMainTest {
//main
public static void main(String[] args) {
// Person p1 = new Person("홍길동", 10, "010-1234-5678");
Person p1 = new Person("홍길동", 10, "010-1234-5678", "F");
}//end of main
}//end of class

속성, 생성자, 메서드 연습
package com.this1;
public class UserInfo {
private String userId;
private String userPassword;
private String userName;
private String userAdress;
private String phoneNumber;
// 생성자 매개변수 1개 ~ 5개, 모두 5가지 만들기 (생성자 오버로딩)
public UserInfo(String userId) {
this.userId = userId;
}
public UserInfo(String userId, String userPassword) {
this(userId);
this.userPassword = userPassword;
}
public UserInfo(String userId, String userPassword, String userName) {
this(userId, userPassword);
this.userName = userName;
}
public UserInfo(String userId, String userPassword, String userName,
String userAdress) {
this(userId, userPassword, userName);
this.userAdress = userAdress;
}
public UserInfo(String userId, String userPassword, String userName,
String userAdress, String phoneNumber) {
this(userId, userPassword, userName, userAdress);
this.phoneNumber = phoneNumber;
}
// getter 메서드
public String getUserId() {
return userId;
}
public String getUserPassword() {
return userPassword;
}
public String getUserName() {
return userName;
}
public String getUserAdress() {
return userAdress;
}
public String getPhoneNumber() {
return phoneNumber;
}
// setter 메서드
public void setUserId(String userId) {
this.userId = userId;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setUserAdress(String userAdress) {
this.userAdress = userAdress;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}// end of class
'Java' 카테고리의 다른 글
| static 변수 개념 (0) | 2025.04.17 |
|---|---|
| 메서드를 활용한 아기 밥주기 게임 (0) | 2025.04.16 |
| 접근 제어 지시자 Accecc Modifier (0) | 2025.04.16 |
| 생성자를 호출해 클래스 상호작용 (0) | 2025.04.16 |
| 메서드를 개념을 활용한 커피머신 프로그램 (1) | 2025.04.16 |