[자바] 체인호출

손경이·2023년 11월 7일
0

자바

목록 보기
4/17

2023.11.07 [테킷 백엔드 장희성 강사님 강의]

체인호출

article
	.setTitle("Hello World!")
    .setWriterName("Paul");
  • 똑같은 article을 쓸 때 앞에꺼 빼도 된다.
  • this로 객체를 반환해야지 체인호출 할 수 있다.
  • 예시로 설명
    • setTitle() 메서드에서 title에 값 집어 넣고 this를 반환하면 된다.
    • 여기서 this는 Article 자기자신을 반환한다.
public class Main {
    public static void main(String[] args) {
        Article article = new Article();

        // 체인호출 적용 안 한 버전
        article.setTitle("Hello World!");
        article.setWriterName("Paul");
        // == 위 아래 코드 같다.
        Article article_ = article.setTitle("Hello World!");
        article_.setWriterName("Paul");

        // == 위 아래 코드 같다.
        // 체인호출 적용 한 버전
        // 앞에 article을 똑같이 쓸 때 생략할 수 있다. - 체인호출
        // 좋고 나쁜 건 없다. 개발자 취향이다.
        article
                .setTitle("Hello World!")
                .setWriterName("Paul");
    }
}

class Article extends Object {
    private String title;
    private String writerName;

    public String getTitle() {
        return title;
    }

    // 체인호출 적용 안 한 버전
    /*
    public void setTitle(String title) {
        this.title = title;
    }
     */

    // 체인호출 적용 한 버전
    public Article setTitle(String title) {
        this.title = title;

        return this; // this : 자기자신을 가리키는 리모콘
    }

    public String getWriterName() {
        return writerName;
    }

    public void setWriterName(String writerName) {
        this.writerName = writerName;
    }

    @Override
    public String toString() {
        return "Article{" +
                "title='" + title + '\'' +
                ", writerName='" + writerName + '\'' +
                '}';
    }
}

0개의 댓글