0
点赞
收藏
分享

微信扫一扫

【java笔记】方法引用介绍和使用


以一个例子说明

public interface FunInterface {
    void prints(String s);
}

public class Demo {
    public static void main(String[] args) {
     print((s)-> System.out.println(s));
    }
    public static void print(FunInterface M){
        M.prints("hello,world");
    }
}

分析:

lambda表达式目的:打印参数传递的字符串,把参数传给System.out对象,调用Out对象的方法println对字符串输出。

使用方法引用优化Lambda表达式:

public class Demo {
    public static void main(String[] args) {
     print(System.out::println);//::参数省略
    }
    public static void print(FunInterface M){
        M.prints("hello,world");
    }
}

方法引用符:
::为引用运算符,所在的表达式被称为“方法引用”

如果lambda要表达的函数方案已经存在于某个方法的实现中,那么则可以通过双冒号来引用该方法作为Lambda的替代者

print((s)-> System.out.println(s));

print(System.out::println);//::参数省略


通过对象名引用成员方法:

例:

接口

@FunctionalInterface
public interface FunInterface {
    void prints(String s);
}

类中定义方法:

public class Myclass {
    public void printUpperCase(String str){
        System.out.println(str.toUpperCase(Locale.ROOT));
    }
}

测试类未使用方法引用:

public class Demo {
    public static void main(String[] args) {
      //通过对象名引用成员方
        Myclass mc=new Myclass();
        printString((s)->mc.printUpperCase(s));
    }
    public static void printString(FunInterface m){
        m.prints("hello");
    }    
}

使用方法引用:

public class Demo {
    public static void main(String[] args) {
      //通过对象名引用成员方
        Myclass mc=new Myclass();
        printString(mc::printUpperCase);
    }
    public static void printString(FunInterface m){
        m.prints("hello");
    }    
}

通过类名引用静态成员方法:

类已经存在,静态成员方法也已经存在

例:计算一个数的绝对值

接口:

@FunctionalInterface
public interface FunInterface {
   int calAbs(int a);
}

 测试类(未使用方法引用):

public class Demo {
    public static void main(String[] args) {
      method(10,(n)->Math.abs(n));
    }
    public static int method(int num,FunInterface m){
        return m.calAbs(num);
    }
}

测试类(使用方法引用):

public class Demo {
    public static void main(String[] args) {
        System.out.println(method(10,Math::abs));
    }
    public static int method(int num,FunInterface m){
        return m.calAbs(num);
    }
}

math类是存在的,abs静态方法也是存在的,直接使用类名::静态方法化简lambda表达式

通过Super引用父类的成员方法:

接口:

public interface Greetable {
    void greet();
}

父类:

//定义父类
public class Human {
    public void sayHello(){
        System.out.println("hello,human");
    }
}

子类:

//子类
public class Man extends Human{
    //子类重写父类sayHello的方法

    @Override
    public void sayHello() {
        System.out.println("hello,man");
    }
    public void method(Greetable g){
        g.greet();
    }
    public void show(){
        method(super::sayHello);
    }

    public static void main(String[] args) {
        new Man().show();
    }
}

通过this引用成员方法:

接口:

public interface Greetable {
    void greet();
}

定义父类:

//定义父类
public class Human {
    public void sayHello(){
        System.out.println("hello,human");
    }
}

子类:

public class Man extends Human{
    //子类重写父类sayHello的方法

    @Override
    public void sayHello() {
        System.out.println("hello,man");
    }
    public void method(Greetable g){
        g.greet();
    }
    public void show(){
        method(this::sayHello);
    }

    public static void main(String[] args) {
        new Man().show();
    }
}

类的构造器引用:

person类:

public class Person {
    private String name;
    public Person(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
}

构造person的抽象类:

public interface gouzao {
    Person personBuilder(String name);
}

测试类: 

public class Demo {
    public static void main(String[] args) {
        PersonBuild("zhangsan",Person::new);
    }
    public static void PersonBuild(String name,gouzao g){
        System.out.println(g.personBuilder(name).getName());
    }
}

数组的构造器引用:

public interface gouzao {
  int[] buliderArray(int length);
}

public class Demo {
    public static void main(String[] args) {
        ArrayBuild(10,int[]::new);
    }
    public static void ArrayBuild(int length,gouzao g){
        System.out.println(g.buliderArray(length).length);
    }
}

举报

相关推荐

0 条评论