方法引用

2021-09-17  本文已影响0人  开心的小哈

1.体验方法引用
::该符号为引用运算符,而他所在的表达式被称为方法引用
无参引用

public interface Add {
    void dd();

}

main

 //方法引用
        userFlu(System.out::println);

    }
    private static void userFlu(Add demo){
        demo.dd();
        System.out.println("呵呵");

    }

带参数引用

//方法引用
        userFlu(System.out::println);

    }
    private static void userFlu(Add demo){
        demo.dd("hhhh");


    }
public interface Add {
    void dd(String s);

}

类名引用静态方法

 //方法引用
        userFlu(MyInerfaceTO::paserInt);
//    userFlu(s -> Integer.parseInt(s));

    }
    private static void userFlu(Add demo){
        int dd = demo.dd("123");
        System.out.println(dd);


    }
public class MyInerfaceTO implements MyInterface{
    public static int paserInt(String s){
     return Integer.parseInt(s);
    }

引用对象的实例方法

 PrintString printString = new PrintString();

        userFlu(printString::printCase);

private static void userFlu(Add demo){
          demo.dd("123");



    }
package test;

/**
 * @outhor chenglong
 * @create 2021-09-15 22:02
 * @name 小哈
 */
public interface Add {
    void dd(String s);

}

package test.interfacei;

/**
 * @outhor chenglong
 * @create 2021-09-17 19:49
 * @name 小哈
 */
public class PrintString {
    public void printCase(String s){
        String s1 = s.toUpperCase();
        System.out.println(s1);

    }
}

引用类的实例方法(成员方法)

public class MyInerfaceTO  implements MyInterface{
    public static MyInerfaceTO init(){
        return new MyInerfaceTO();
    }
    
    public static int paserInt(String s){
     return Integer.parseInt(s);
    }
    public  String subString(String m,int s,int y){
        return m.substring(s,y);
    }
package test;

/**
 * @outhor chenglong
 * @create 2021-09-15 22:02
 * @name 小哈
 */
public interface Add {
    String  mySubString(String s,int x,int y);

}

      userFlu(MyInerfaceTO.init()::subString);
    }
    private static void userFlu(Add demo){
        String hoole = demo.mySubString("hoole", 2, 3);
        System.out.println(hoole);
    }

引用构造器

  userFlu(PrintString::new);
//        userFlu((name,age)->new PrintString(name,age));
        //lambda表达式被构造方法代替的时候,他的形式参数全部传递构造器作为参数
    }
    private static void userFlu(Add demo){
        PrintString lin = demo.iBringtString("林檎下", "12");
        System.out.println(lin.toString());
    }
package test;

import test.interfacei.PrintString;

/**
 * @outhor chenglong
 * @create 2021-09-15 22:02
 * @name 小哈
 */
public interface Add {
    PrintString iBringtString(String name,String age);

}

package test.interfacei;

/**
 * @outhor chenglong
 * @create 2021-09-17 19:49
 * @name 小哈
 */
public class PrintString {
  String name;
  String age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public PrintString(String name, String age) {
        this.name = name;
        this.age = age;
    }
}

上一篇下一篇

猜你喜欢

热点阅读