方法引用通过方法的名字来指向一个方法。
所谓的方法引用实际上指的是将一个特定类的方法功能映射过来,而后通过接口中的方法,利用lamda表达式实现方法体的定义,当然这种定义的形式一共分为四种:
·类中静态方法的引用:类名称::静态方法名称;
·类中普通方法的引用:实例化对象名称::普通方法;
·类之中构造方法的引用:类名称::new;
·特定类型的任意方法引用:类名称::方法名称。
类中静态方法的引用
以String中的 public static String valueOf(int i) 方法为例:
此时定义的接口就需要设置输入值以及返回值,那么为了让接口更加适合于各种方法,使用泛型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.devnp;
public class JavaMethodReference {
public static void main(String [] args){ MethodReference<String, Integer> methodReference = String::valueOf ; System.out.println(methodReference.getInfo(2000));
} }
interface MethodReference<R, P>{ public R getInfo(P p); }
|
类中普通方法的引用
以String中的 public String toLowerCase(Locale locale) 方法为例:
此时定义的接口就需要设置输入值以及返回值,那么为了让接口更加适合于各种方法,使用泛型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.devnp;
import java.util.Locale;
public class JavaMethodReference {
public static void main(String [] args){
String str = "hello world!"; MethodReference<String, Locale> methodReference1 = str::toUpperCase; System.out.println(methodReference1.getInfo(Locale.ENGLISH));
} }
interface MethodReference<R, P>{ public R getInfo(P p); }
|
类之中构造方法的引用
创建 Person 对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package com.devnp;
public class JavaMethodReference {
public static void main(String [] args){
Creator<Person> personCreator = Person::new; System.out.println(personCreator.create("Lao Wang", 23));
} }
interface Creator<R extends Person>{ public R create(String name, Integer age); }
class Person{
private String name ;
private Integer age ;
public Person(String name, Integer age) { this.name = name; this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
|
特定类型的任意方法引用
以String中的 public int compareToIgnoreCase(String str) 方法为例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.devnp;
import java.util.Arrays; import java.util.Comparator;
public class JavaMethodReference {
public static void main(String [] args){
String[] stringArray = { "Barbara", "Linda", "James", "Mary", "Lucy" }; Arrays.sort(stringArray, String::compareToIgnoreCase);
System.out.println(Arrays.toString(stringArray)); } }
|
完整示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| package com.devnp;
import java.util.Arrays; import java.util.Comparator; import java.util.Locale;
public class JavaMethodReference {
public static void main(String [] args){ MethodReference<String, Integer> methodReference = String::valueOf ; System.out.println(methodReference.getInfo(2000));
String str = "hello world!"; MethodReference<String, Locale> methodReference1 = str::toUpperCase; System.out.println(methodReference1.getInfo(Locale.ENGLISH));
Creator<Person> personCreator = Person::new; System.out.println(personCreator.create("Lao Wang", 23));
String[] stringArray = { "Barbara", "Linda", "James", "Mary", "Lucy" }; Arrays.sort(stringArray, String::compareToIgnoreCase);
System.out.println(Arrays.toString(stringArray)); } }
interface MethodReference<R, P>{ public R getInfo(P p); }
interface Creator<R extends Person>{ public R create(String name, Integer age); }
class Person{
private String name ;
private Integer age ;
public Person(String name, Integer age) { this.name = name; this.age = age; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Integer getAge() { return age; }
public void setAge(Integer age) { this.age = age; }
@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
|
运行结果:
1 2 3 4
| 2000 HELLO WORLD! Person{name='Lao Wang', age=23} [Barbara, James, Linda, Lucy, Mary]
|
Author:
Darren Du
License:
Copyright (c) 2019 MIT LICENSE