[CHAPTER 3] - 람다표현식 part2 질문 #39
-
질문 1책에서 p.119를 보면 // 생성자 참조
Supplier<Apple> c1 = Apple::new; // -> 1번
Apple a1 = c1.get(); // -> 2번 이 코드에서 2번 부분에서 Apple 객체가 만들어진다고 되어있습니다. 그리고 위의 코드를 람다로 바꾸면 // 람다
Supplier<Apple> c1 = () -> new Apple(); // -> 1번
Apple a1 = c1.get(); // -> 2번 이렇게 되었고 //기본 코드
class AppleSupplier implements Supplier<Apple> {
@Override
public Apple get() {
return new Apple();
}
}
Supplier<Apple> c1 = new AppleSupplier();
Apple a1 = c1.get(); 하지만 또 이렇게 바꿔보니 Apple객체가 왜 생성자 참조나 람다식의 2번 코드에서 생성되는 것인지는 어느 정도 이해가 갑니다. 그러면 // 생성자 참조
Supplier<Apple> c1 = Apple::new;
// 람다
Supplier<Apple> c1 = () -> new Apple(); 위의 코드들은 기본 코드에서 오버라이드 부분과 |
Beta Was this translation helpful? Give feedback.
Answered by
JoisFe
Jun 27, 2023
Replies: 2 comments
-
//기본 코드
class AppleSupplier implements Supplier<Apple> {
@Override
public Apple get() {
return new Apple();
}
}
Supplier<Apple> c1 = new AppleSupplier();
// 생성자 참조
Supplier<Apple> c1 = Apple::new;
// 람다
Supplier<Apple> c1 = () -> new Apple();
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Supplier c1 = Apple::new; 는 Supplier의 추상 메서드를 정의한 것 뿐이구요 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
ahngilwoong
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Supplier c1 = Apple::new; 는 Supplier의 추상 메서드를 정의한 것 뿐이구요
get을 써야 new Apple()이 되는 것으로 이해하면될 것 같습니다.