JDK1.8新特性【之】方法引用(定义、解析、使用)
1.方法引用
- 方法引用
- 是lambda表达式的进一步简写,存在三种形式
- 1.类名::静态方法 ,满足 lambda表达式右侧只有一行,参数与返回值可以对应。
- 2.实例名::方法,满足 lambda表达式右侧只有一行,参数与返回值可以对应。
- 3.类名::实例方法,满足 lambda表达式右侧只有一行,参数与返回值可以对应。且 只有两个参数,并且参数1作为调用者,参数2作为参数,且顺序不能改变
2.定义接口函数与测试类

3.三种引用形式与简写

4.完整源码
import java.util.function.Consumer;
public class _Test {
public static void main(String[] args) {
Consumer<Object> c1 = (a) -> {
Method.test2(a);
};
Consumer<Object> c1_ = Method::test2;
Method method = new Method();
Consumer<Object> c2 = (a) -> {
method.test1(a);
};
Consumer<Object> c2_ = method::test1;
Face03<Method, String> face03 = (t, r) -> {
t.test1(r);
};
Face03<Method, String> face03_ = Method::test1;
}
@FunctionalInterface
interface Face03<T, R> {
void test1(T t, R r);
}
static class Method {
public void test1(Object a) {
}
public static void test2(Object a) {
}
}
}
《目录:Java基础》
《目录:JDK1.8新特性》
《幕》
- 留白 —<老吉>
- ~ 今 ~ ❀ ~ ❀❀❀❀❀❀❀❀❀❀ ❀❀❀❀❀❀❀❀❀❀ ❀❀❀❀❀❀❀