函数接口方法的的四大引用

解析:让左边的函数跟右边的方法具有相同的逻辑,前提条件是两边具有相同的 参数列表跟返回类型 以下所有的引用都是具备这样的条件
以下所有的接口都是jdk1.8以上的util包下的function包里面的

1,引用类的静态方法 类名::静态方法名

public void method1(){
		Consumer<String> c = Student::play ;
		c.accept("王者荣耀"); 
		
		Function<Integer, String>  f =  String::valueOf ;//将整形转成字符串,这里String是一个类
		String s = f.apply(18);
		System.out.println(s);
}

2,引用某个对象的普通方法 对象名::方法名

public void method2(){
		Student s = new Student();
		Consumer<Integer> c = s::study  ;  
		c.accept(30);		
}

3,引用某个类的构造方法 类::new

public void method3(){
		Supplier<Student> s = Student::new; 
		Student s1 = s.get();
		System.out.println(s1);
	}

4,引用任意对象的方法 类名::方法名

public void method4(){
		Student s = new Student();
		BiFunction<Student, Integer, String> bf = Student::sleep  ; 
		String s1 = bf.apply(s, 30);
		System.out.println(s1); 
	}

类里面的几种方法

package com.blb.seven03;

public class Student {
	//一个参数无返回
	public void study(int time){
		System.out.println("好好学习,天天向上"+time+"分钟");
	}
	//一个参数有返回
	public String sleep(int time){
		System.out.println("早睡早起身体好"+time+"分钟");
		return "hello";
	}
	//一个参数无返回静态
	public static void  play(String name){
		System.out.println("一起玩"+name);   
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值