How does method reference casting work?

后端 未结 3 1745
走了就别回头了
走了就别回头了 2021-01-06 10:57
public class Main {
    interface Capitalizer {
        public String capitalize(String name);
    }

    public String toUpperCase() {
        return \"ALLCAPS\";
          


        
3条回答
  •  难免孤独
    2021-01-06 11:53

    You should change:

    public String toUpperCase()
    

    to

    public static String toUpperCase(String text)
    

    You should read the java tutorial on method references. The different kind of method references and there is a similar example with String::compareToIgnoreCase (Reference to an Instance Method of an Arbitrary Object of a Particular Type).

    The equivalent lambda expression for the method reference String::compareToIgnoreCase would have the formal parameter list (String a, String b), where a and b are arbitrary names used to better describe this example. The method reference would invoke the method a.compareToIgnoreCase(b).

提交回复
热议问题