函数引用

函数引用必须保证所用接口与引用的方法的 形参和返回值类型一致
让左边的函数跟右边的方法具有相同的逻辑,前提条件是两边具有相同的参数列表跟返回类型

  1. 引用类的静态方法 格式 类名::方法名
    例如:
	public void method1(){
		//1,引用类的静态方法    类名::静态方法名
		Consumer<String> c = Student::play;
		c.accept("和平精英");
		
		Function<Integer, String>  f =  String::valueOf ;
		String s=f.apply(15);
		System.out.println(s);
		
	}
  1. 引用某个对象的普通方法 对象名::方法名
    例如:
	public void method2(){
		//2,引用某个对象的普通方法 		对象名::方法名
		Student s =  new Student();
		Consumer<Integer> c = s::study;
		c.accept(18);	
		//定义具有   System.out.println
		Consumer<String> c1 =System.out::println;
		c1.accept("发快递费南");
		
	}
  1. 引用类的构造方法 类名::new
public void method3(){
		//3,引用某个类的构造方法    类::new
		Supplier<Student> s = Student::new;
		Student s1 = s.get();	
	}
  1. 引用任意对象的方法 类名::方法名
public void method4(){
		//4,引用任意对象的方法       类名::方法名
		Student s = new Student();
		BiFunction<Student, Integer, String> bf = Student::sleep  ; 	
		bf.apply(s, 18);
		
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值