主要参考: 【java8新特性】方法引用
引入
方法引用是jdk 1.8中的新特性,通过与lambda结合可以进一步简化代码
在使用中主要有以下四种形式:
- 静态方法引用 ClassName::staticMethod
- 类::实例方法 ClassName::instanceMethod
- 对象::实例方法 Object::instanceMethod
方法引用通常是用在赋给已经写好的函数式接口
他会生成一个函数式接口的实例,通过这个引用你可以调用接口中的方法
一. 静态方法引用
public interface Inter {
String handleString(String a,String b);
}
public class OneClass {
public static String contact(String a,String b){
return a+b;
}
public class lambdaTest {
public static void main(String[] args) {
Oneclass oneclass=new OneClass();
Inter one=OneClass::contact;//静态引用
Inter two=oneclass::contact;//对象,此时需要把contact改为非静态
String result2=two.handleString("adfs","dfs");
String result= one.handleString("as","ad");
System.out.println(result);
sout(result2);
Inter two=(String a,String b)->OneClass.contact(a,b);
result=two.handleString("asfd","adf");
System.out.println(result);
}
}
方法引用直接赋给接口变量,则此接口变量就引用了一个接口实例。
二.对象::实例方法引用
@FunctionalInterface
public interface ImTheOne {
String handleString(String a, String b);
}
class OneClass {
public String concatString(String a, String b) {
return a + b;
}
}
public class Test {
public static void main(String[] args) {
OneClass oneClass = new OneClass();
ImTheOne theOne = oneClass::concatString;
String result = theOne.handleString("abc", "def");
System.out.println(result);
//相当于以下效果
OneClass oneClass2 = new OneClass();
ImTheOne theOne2 = (a, b) -> oneClass2.concatString(a, b);
String result2 = theOne2.handleString("123", "456");
System.out.println(result2);
}
}
三.类::实例方法
此种引用较为复杂,当一个对象调用方法时,方法的某个参数是函数式接口,而接口的方法参数列表的第一个参数就是调用者所属的类时,可以用类::实例方法的写法赋给此函数式接口。
public interface ImTheOne<T> {
String handleString(T a, String b);
}
class OneClass {
String oneString;
public String concatString(String a) {
return this.oneString + a;
}
public String startHandleString(ImTheOne<OneClass> imTheOne, String str) {
String result = imTheOne.handleString(this, str);
return result;
}
}
public class Test {
public static void main(String[] args) {
OneClass oneClass = new OneClass();
oneClass.oneString = "abc";
String result = oneClass.startHandleString(OneClass::concatString, "123");
System.out.println(result);
//相当于以下效果
OneClass oneClass2 = new OneClass();
oneClass2.oneString = "abc";
ImTheOne theOne2 = (a, b) -> oneClass2.concatString(b);
String result2 = theOne2.handleString(theOne2, "123");
System.out.println(result2);
}
}
例二:
public class Student
{
private String name;
private Integer score;
public void setNameAndScore(String name, Integer score)
{
this.name = name;
this.score = score;
System.out.println("Student "+ name +"'s score is " + score);
}
public static void main(String[] args)
{
/*lambda表达式的用法:
TestInterface testInterface = (student, name, score) -> student.setNameAndScore(name, score);*/
//类的任意对象的实例方法引用的用法:
TestInterface testInterface = Student::setNameAndScore;
testInterface.set(new Student(), "DoubleBin", 100);
}
@FunctionalInterface
interface TestInterface
{
// 注意:入参比Student类的setNameAndScore方法多1个Student对象,除第一个外其它入参类型一致
public void set(Student d, String name, Integer score);
}
}
总结:
①方法引用的通用特性:方法引用的参数列表和返回值和函数式接口中的方法参数列表和返回值相同。
②函数式接口方法的参数列表的第一个参数会作为引用方法的隐式参数,即为该类的引用变量,例如String::compareToignoreCase等价于(x,y)->x.compareToIgnoreCase(y);其实就是接口方法多一个对象引用变量
构造器引用
与方法引用很类似,只不过方法名为new,例如Person::new是Person构造器的一个引用。
public interface ImTheOne {
TargetClass getTargetClass(String a);
}
class TargetClass {
String oneString;
public TargetClass() {
oneString = "default";
}
public TargetClass(String a) {
oneString = a;
}
}
public class Test {
public static void main(String[] args) {
ImTheOne imTheOne = TargetClass::new;
TargetClass targetClass = imTheOne.getTargetClass("abc");
System.out.println(targetClass.oneString);
//相当于以下效果
ImTheOne imTheOne2 = (a) -> new TargetClass("abc");
TargetClass targetClass2 = imTheOne2.getTargetClass("123");
System.out.println(targetClass2.oneString);
}
到集合部分此种方法会有很大的方便。
数组::new
也算构造器引用的一种
@FunctionalInterface
public interface ImTheOne<T> {
T getArr(int a);
}
public class Test {
public static void main(String[] args) {
ImTheOne<int[]> imTheOne = int[]::new;
int[] stringArr = imTheOne.getArr(5);
System.out.println(stringArr.length);
}
此类引用需要注意的是函数式接口的方法必须有一个参数,而且只能有一个,代表未来的数组长度。
本文详细介绍了Java 1.8中的方法引用,包括静态方法引用、对象实例方法引用和类实例方法构造器引用。通过实例展示了如何使用这些引用简化代码,并与lambda表达式结合使用。此外,还提到了数组的`new`引用,即构造器引用。文章强调了方法引用与函数式接口的关系,以及它们在参数列表和返回值上的匹配性。
1557

被折叠的 条评论
为什么被折叠?



