@@ -87,3 +87,76 @@ public class A{
87
87
}
88
88
```
89
89
90
+
91
+
92
+ #### 메소드 참조(Method References)
93
+
94
+ + 메소드 참조 : 메소드를 참조해서 매개 변수의 정보 및 리턴 타입을 알아내어, 람다식에서 불필요한 매개 변수를 제거하는 것이 목적이다.
95
+
96
+ ``` java
97
+ 람다식이 기존 메소드를 단순히 호출만 하는 경우
98
+ Math 클래스의 max() 정적 메소드를 호출하는 람다식
99
+ (left,right) - > Math . max(left,right);
100
+ ```
101
+
102
+ > -> Math :: max()
103
+
104
+
105
+
106
+ + 정적 메소드를 참조할 경우
107
+ + 클래스 이름 뒤에 :: 기호를 붙이고 정적 메소드 이름을 기술
108
+ + 클래스 :: 메소드
109
+
110
+
111
+
112
+ ``` java
113
+ public class Calculator {
114
+
115
+ public static int staticMethod (int x , int y ) {
116
+ return x + y;
117
+ }
118
+
119
+ public int instanceMethod (int x , int y ) {
120
+ return x + y;
121
+ }
122
+ }
123
+ ```
124
+
125
+
126
+
127
+ ``` java
128
+ import java.util.function.IntBinaryOperator ;
129
+
130
+ public class MethodReferencesExample {
131
+ public static void main (String [] args ) {
132
+ IntBinaryOperator operator;
133
+
134
+ // 정적메소드 참조
135
+ operator = (x, y) - > Calculator . staticMethod(x, y);
136
+ System . out. println(" 결과1: " + operator. applyAsInt(1 , 2 ));
137
+
138
+ operator = Calculator :: staticMethod;
139
+ System . out. println(" 결과2: " + operator. applyAsInt(3 , 4 ));
140
+
141
+ // 인스턴스 메소드 참조
142
+ Calculator obj = new Calculator ();
143
+ operator = (x, y) - > obj. instanceMethod(x, y);
144
+ System . out. println(" 결과3: " + operator. applyAsInt(5 , 6 ));
145
+
146
+ operator = obj:: instanceMethod;
147
+ System . out. println(" 결과4: " + operator. applyAsInt(7 , 8 ));
148
+
149
+ }
150
+ }
151
+ ```
152
+
153
+
154
+
155
+ #### 생성자 참조
156
+
157
+ + 메소드 참조는 생성자 참조도 포함한다.
158
+ + 단순히 객체를 생성하고 리턴하도록 구성된 람다식은 생성자 참조로 대치할 수 있다.
159
+ + (a,b) -> (return new 클래스(a,b);) -> 생성자 참조로 표헌 -> 클래스 :: new
160
+
161
+
162
+
0 commit comments