인수 전달 방법

정적 멤버

클래스 복사

package ex14.example1;

class User {
    private int id;
    private String name;
    private String tel;

    public User(User user) {
        this.id = user.getId();
        this.name = user.getName();
        this.tel = user.getTel();
    }

    public User(int id, String name, String tel) {
        this.id = id;
        this.name = name;
        this.tel = tel;
    }

    @Override
    public String toString() {
        return "User{" +
                "id= " + id +
                ", name= '" + name + '\\'' +
                ", tel= '" + tel + '\\'' +
                '}';
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getTel() {
        return tel;
    }
}

public class CopyEx04 {
    public static void main(String[] args) {
        User u1 = new User(1, "ssar", "010-1111-2222");
        System.out.println(u1.toString());

        // 1. 통째로 복사
        User u2 = new User(u1);
        System.out.println(u2.toString());

        // 2. 부분변경 복사
        User u3 = new User(u1.getId(), u1.getName(), "010-2222-3333");
        System.out.println(u3.toString());
    }
}

Untitled

클래스를 다른 클래스에 복사 하면서 부분변경 복사함

package ex14.example1;

import java.time.LocalDateTime;
import java.util.Locale;

// Data Transfer Object
class JoinDTO {
    private String username; // ssar(id)
    private String password;
    private String email;

    public JoinDTO(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }
}

class Member {
    private int no = 1; // 1, 2, 3 ...
    private String username; // ssar(id)
    private String password;
    private String email;
    private LocalDateTime createApp; // 생성 시간 (날짜) 찍히게 하기;

    public Member(int no, JoinDTO dto) {
        this.no = no;
        this.username = dto.getUsername();
        this.password = dto.getPassword();
        this.email = dto.getEmail();
        this.createApp = createApp;
    }

    @Override
    public String toString() {
        return "Member{" +
                "no=" + no +
                ", username='" + username + '\\'' +
                ", password='" + password + '\\'' +
                ", email='" + email + '\\'' +
                ", createApp=" + createApp +
                '}';
    }
}

public class CopyEx05 {
    public static void main(String[] args) {
        JoinDTO d1 = new JoinDTO("ssar", "1234", "[email protected]");
        JoinDTO d2 = new JoinDTO("cos", "1234", "[email protected]");

        //현재 시간
        LocalDateTime now = LocalDateTime.now();

        //joindto(d1, d2)를 member에 옮기기
        Member m1 = new Member(1, d1);
        Member m2 = new Member(2, d2);

        System.out.println(m1.toString());
        System.out.println(m2.toString());
    }
}

Untitled