#36 rxjava: method reference

This commit is contained in:
haerong22
2023-04-28 00:32:13 +09:00
parent d866cc3d1b
commit eaf7da0fd9
5 changed files with 110 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
package org.example.ex07;
public class CarInventory {
private int incomingCount;
private int totalCount;
public CarInventory(int totalCount) {
this.totalCount = totalCount;
}
public int getExpectedTotalCount(int incomingCount){
return totalCount + incomingCount;
}
}

View File

@@ -0,0 +1,18 @@
package org.example.ex07;
import java.util.function.Function;
/**
* Class Name::instance method 메서드 레퍼런스 예
*/
public class ClassNameInstanceMethodEx {
public static void main(String[] args) {
Function<Car, String> f1 = car -> car.getCarName();
String carName1 = f1.apply(new Car("트래버스"));
System.out.println(carName1);
Function<Car, String> f2 = Car::getCarName;
String carName2 = f2.apply(new Car("팰리세이드"));
System.out.println(carName2);
}
}

View File

@@ -0,0 +1,26 @@
package org.example.ex07;
import java.util.function.Function;
/**
* Class Name::static method 메서드 레퍼런스 예
*/
public class ClassNameStaticMethodEx {
public static void main(String[] args) {
// 람다 표현식 메서드 레퍼런스로 축약 전
Function<String, Integer> f1 = (String s) -> Integer.parseInt(s);
Integer result1 = f1.apply("3");
System.out.println(result1);
// 람다 표현식 메서드 레퍼런스로 축약 전
Function<String, Integer> f2 = s -> Integer.parseInt(s);
Integer result2 = f2.apply("3");
System.out.println(result2);
// 람다 표현식을 메서드 레퍼런스로 축약
Function<String, Integer> f3 = Integer::parseInt;
Integer result3 = f3.apply("3");
System.out.println(result3);
}
}

View File

@@ -0,0 +1,20 @@
package org.example.ex07;
import org.example.common.Car;
import java.util.function.Function;
/**
* Constructor::new 예
*/
public class ConstructorReferenceEx {
public static void main(String[] args) {
Function<String, Car> f1 = s -> new Car(s);
Car car1 = f1.apply("콜로라도");
System.out.println(car1.getCarName());
Function<String, Car> f2 = Car::new;
Car car2 = f2.apply("카니발");
System.out.println(car2.getCarName());
}
}

View File

@@ -0,0 +1,32 @@
package org.example.ex07;
import java.util.function.Function;
import java.util.function.IntUnaryOperator;
import java.util.function.UnaryOperator;
/**
* Object::instance method 예
*/
public class ObjectInstanceMethodExample {
public static void main(String[] args) {
final CarInventory carInventory = new CarInventory(10);
Function<Integer, Integer> f1 = count -> carInventory.getExpectedTotalCount(count);
int totalCount1 = f1.apply(10);
System.out.println(totalCount1);
Function<Integer, Integer> f2 = carInventory::getExpectedTotalCount;
int totalCount2 = f2.apply(20);
System.out.println(totalCount2);
// T -> T
UnaryOperator<Integer> f3 = carInventory::getExpectedTotalCount;
int totalCount3 = f3.apply(30);
System.out.println(totalCount3);
// Integer -> Integer
IntUnaryOperator f4 = carInventory::getExpectedTotalCount;
int totalCount4 = f4.applyAsInt(40);
System.out.println(totalCount4);
}
}