1
0

More than 1 year has passed since last update.

method refference of particular type

Last updated at Posted at 2023-01-31

type::methodのメソッド参照

instance::methodではなく、type::methodを渡すタイプ。
instanceを渡さないので、method発行する側が
first argumentにtypeのinstanceを指定する。
下記のsampleで、SampleClass#apply(String u)は、single argumentだが
SampleClass::applyのようにメソッド参照指定した場合、apply発行側が
SampleClassのinstanceを指定する。instanceは、first argumentに追加される
もともとfirst argumentであるString uは、押し出される形でsecond argumentになる。最終的にapply(SampleClass s,String u)。
そして、methodは、double argumentとなり、発行側としてはBiFunctionとなる
Supplier(noarg,exist return)であればFunction(one arg,exist return)となる。

この仕組みはqiitaで以下のQ&Aを起票し、@uasi氏より回答をいただき、理解することができた
https://qiita.com/Natukasi/questions/38e42198c0a558b48e34

class SampleClass {
    String str;
    SampleClass(String str) {
        this.str = str;
    }
    public String apply(String u) {
        return str + ":" + u;
    }
}
public class Outer {
    public static void main(String[] args) {
        List<String> l = List.of("sato-a","sato-b","sato-c");
        for(String str:l) {
            String ret = method(SampleClass::apply,str);
            System.out.println(ret);
        }
    }
    static String method(BiFunction<SampleClass,String,String> f, String str) {
        return f.apply(new SampleClass("xxx"),str);
    }
}
xxxsato-a
xxxsato-b
xxxsato-c
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0