目录
一、方法引用
1、目的
简化Lambda表达式;
2、简单演示
函数式接口:
package study.method_references;
@FunctionalInterface
public interface myPrint {
void print(String s);
}
测试类:
package study.method_references;
public class Test {
public static void main(String[] args) {
String s = "打印我哦!";
print(System.out::println,s);//打印我哦!
}
private static void print(myPrint myPrint,String s){
myPrint.print(s);
}
}
3、两种写法
Lambda表达式写法:
s -> System.out.println(s);
方法引用写法:
System.out::println;
效果完全一致;
二、通过对象名引用成员方法
代码演示:
函数式接口myPrint:
package study.method_references;
@FunctionalInterface
public interface myPrint {
void print(String s);
}
实体类TestA:
package study.method_references;
public class TestA {
public void printA(String s){
System.out.println(s);
}
}
测试类Test:
package study.method_references;
public class Test {
public static void main(String[] args) {
//一般写法
String string = "打印我哦!";
print((s)->{
TestA testA = new TestA();
testA.printA(s);//打印我哦!
},string);
//通过对象名引用方法
TestA testB = new TestA();
print(testB::printA,string);//打印我哦!
}
private static void print(myPrint myPrint,String s){
myPrint.print(s);
}
}
三、通过类型引用静态方法
函数式接口myPrint:
package study.method_references;
@FunctionalInterface
public interface myPrint {
void print(String s);
}
实体类TestB:
package study.method_references;
public class TestB {
public static void printB(String s){
System.out.println(s);
}
}
测试类Test:
package study.method_references;
public class Test {
public static void main(String[] args) {
//一般写法
String string = "打印我哦!";
print((s)->{
TestB.printB(s);//打印我哦!
},string);
//通过类引用静态方法
print(TestB::printB,string);//打印我哦!
}
private static void print(myPrint myPrint,String s){
myPrint.print(s);
}
}
四、使用super引用父类的成员方法
父类:
package study.method_references;
public class Fu {
public void say(){
System.out.println("父类的say");
}
}
接口:
package study.method_references;
public interface myMethod {
void doSth();
}
子类兼测试类:
package study.method_references;
public class Zi extends Fu {
@Override
public void say(){
System.out.println("子类的say");
}
public static void main(String[] args) {
new Zi().show();//父类的say
}
private static void myDO(myMethod myMethod){
myMethod.doSth();
}
private void show(){
myDO(super::say);
}
}
五、使用this引用本类成员变量
将【四】中的super改成this即可;
六、类的构造器(构造方法)的引用
Person类:
package study.method_references;
public class Person {
private String name;
public Person(String name){
this.setName(name);
}
public Person(){
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
函数式接口PersonBuilder:
package study.method_references;
@FunctionalInterface
public interface PersonBuilder {
Person buildPerson(String name);
}
测试类Main:
package study.method_references;
public class Main {
public static void main(String[] args) {
printName("大哥",Person::new);//带参构造
}
private static void printName(String name, PersonBuilder pb){
Person person = pb.buildPerson(name);
System.out.println(person.getName());
}
}
七、数组的构造器引用
函数式接口:
package study.method_references;
@FunctionalInterface
public interface ArrayBuilder {
int[] buildArray(int len);
}
测试类:
package study.method_references;
public class ArrayBuilderTest {
public static void main(String[] args) {
goOn((len)->{
//传统写法
return new int[len];
},10);
//引用写法
goOn(int[]::new,10);
}
private static void goOn(ArrayBuilder arrayBuilder,int len){
int[] arr = arrayBuilder.buildArray(len);
System.out.println(arr.length);
}
}