Created
October 11, 2020 07:22
-
-
Save RayStarkMC/6f56832dc4e0b188f44a47807adf8585 to your computer and use it in GitHub Desktop.
キャプチャメソッドの使用例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
Function<?, ?> f = Function.identity(); | |
Object obj = new Object(); | |
// これは出来ない | |
// f.apply((?)obj); | |
//これは出来る | |
Object obj2 = captureMethod(f, obj); | |
} | |
@SuppressWarnings("unchecked") | |
private static <T, R> Object captureMethod(Function<T, R> f, Object o) { | |
//ここで無検査キャスト(ただしoがT型だと証明できる場合に限る!!!!) | |
return f.apply((T)o); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment