Skip to content

생성자 참조를 위한 함수형 인터페이스 생성 #47

Discussion options

You must be logged in to vote

질문하신 의도를 파악해 보면은 TriFunction으로 새로운 함수형 인터페이스를 구현하는 것 이외에
자바에서 제공하는 Funciton Interface 만으로 구현할 수 없는지에 대한 질문의도로 파악하고 답변을 드려보겠습니다.

먼저 Color 클래스는 아래와 같은 구조로 되어있습니다.

package com.hello.modern.chapter3.domain;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@ToString
public class Color {

    private final int red;
    private final int green;
    private final int blue;

    public Color(int[] colors) {
        this.red = colors[0];
        this.green = colors[1];
        this.blue =  colors[2];
    }
}

Function을 통해 Color클래스를 생성하는 방법은 아래와 같습니다.

Function<int[], Color> colorFactory = Color::new;
Color color = colorFactory.apply(new int[]{255, 0, 0}); // red
System.out.println(color

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by coalong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
coalong 아영님을 위한 라벨입니다. Chapter 3 Chapter 3 정리를 위한 라벨입니다.
2 participants