JAVA

this 사용법

jiyoon12 2025. 4. 24. 00:46
  • 인스턴스 자신의 메모리를 가리킨다.
  • 생성자에서 또 다른 생성자를 호출 할 때 사용한다
  • 자신의 주소(참조 값)을 반환 한다. 
package com._this;

/**
 * 클래스 설계 하는 측 코드
 */
public class Person {

    // this에 3가지 사룡법이 존재한다.
    // 1. 자기 자신의 주소를 가리킨다.
    // 2. 생성자에서 다른 생성자를 호출 할 때 사용할 수 있다.
    // 3. 자신의 주소를 반환 시킬 수 있다.

    private String name;
    private int age;
    private String phone;
    private String gender; // F, M

    // 첫번째 생성사 사용 방법 this.
    // 1. 사용 방법(문법, 왜)
    public Person(String name, int age) {
        // 매개변수 = 매개변수
        // 멤버변수 = 매개변수
        this.name = name;
        this.age = age;
        System.out.println("1번 생성자 호출 됨");
    }

    // 2. 사용 방법 this();
    // this 는 생성자에서 다른 생성자를 호출 할 수 있다.
    public Person(String name, int age, String phone) {
        // System.out.println("111111111111111");
//        this.name = name;
//        this.age = age;
        this(name, age);
        this.phone = phone;
        System.out.println("2번 생성자 호출 됨");
    }

    // 3. 사용 방법
    public Person(String name, int age, String phone, String gender) {
        this(name, age, phone);
        this.gender = gender;
        System.out.println("3번 생성자 호출 됨");
    }
}
package com._this;

public class PersonMainTest {

    public static void main(String[] args) {

        //Person p1 = new Person("홍길동",10,"010-1234-1234");
        Person p1 = new Person("홍길동",10,"010-1234-1234","F");
    }
}

package com._this;

public class UserInfo {

    private String userId;
    private String userPassword;
    private String userName;
    private String userAddress;
    private String phoneNumber;

    // 생성자 매개변수 1개 ~5개 즉, 생성자가 5개 만들어 주세요(생성자 오버 로딩)

    public UserInfo(String userId) {
        this.userId = userId;
    }

    public UserInfo(String userId, String userPassword) {
        this.userId = 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 userAddress) {
        this(userId, userPassword, userName);
        this.userAddress = userAddress;
    }

    public UserInfo(String userId, String userPassword, String userName, String userAddress, String phoneNumber) {
        this(userId, userPassword, userName, userAddress);
        this.phoneNumber = phoneNumber;
    }

    // getter 메서드
    public String getUserId() {
        return userId;
    }

    public String getUserPassword() {
        return userPassword;
    }

    public String getUserName() {
        return userName;
    }

    public String getUerAddress() {
        return userAddress;
    }

    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 setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}