Home  >  Article  >  Java  >  An in-depth analysis of method references in Java

An in-depth analysis of method references in Java

WBOY
WBOYforward
2022-06-13 17:55:192353browse

This article brings you relevant knowledge about java, which mainly introduces related issues about method reference. Everyone knows the method, which is the method we define when writing code. The method reference is what is used to reference this method. The reference method makes it clear that its purpose is to further optimize Lambda expressions, thereby reducing the code to be written more simply. I hope it will be helpful to everyone.

An in-depth analysis of method references in Java

Recommended study: "java video tutorial"

What is a method reference?

In fact, we start to understand it literally. Everyone knows the method, which is the method we define when writing code. The method reference is what is used to reference this method. The reference method makes it clear that its purpose is to further optimize Lambda expressions, thereby reducing the code to be written more simply. right! You heard it right, Lambda expressions are already very optimized, so how can they be optimized? When the corresponding class, object, super, and this appear in our code, we can use method references. The premise of this method reference is that we have Lambda expressions. So how is it used? Let’s continue looking down.

Method Quotation

Since the title is a method quotation, what is a method quotation? The method reference operator is double colon [::], which is a method reference, and this is also a new syntax, a reference operator, through which method references are implemented. of. If the function solution to be expressed by Lambda already exists in the implementation of a method, we can use a double colon to reference the method to replace Lambda.

Note: The parameters passed in Lambda must be of a type acceptable to the method in the method reference, otherwise an exception will be thrown.

How to use method references?

Method references can be used in the following ways:

An in-depth analysis of method references in Java

Since there are method references in the above ways, let’s study them one by one. Just give it a try.

Referring to member methods through object names

So how to reference methods through object names? We know that objects are created through classes, so we first need to create a class, then define a member method in the class, then create an object through the class, and use pairs to reference the member method.

For example:

Define a member method, pass a string, and output the string in uppercase letters

We put the above requirements into Let’s implement it.

Define a class first

public class Demo02MethodRerObject {

//定义一个成员方法,传递字符串,把字符串按照大写输出

public void printUpperCaseString(String s){

System.out.println(s.toUpperCase());

}

}

Since it is output, we need to print it out, and using Lambdab requires us to define a functional interface for printing, and define the printing string in the functional interface abstract method.

/*

定义一个打印的函数式接口

*/

@FunctionalInterface

public interface Printable {

//定义打印字符串的抽象方法

void print(String s);

}

and reference the membership method through the object name. The premise is that the object name already exists, and the member method also exists. You can use the object name to quote the member method. Let's write it in code: First, we use Lambda to write this requirement, and then we use method references to optimize Lambda.

public class Demo03ObjectMethodReference {

//定义一个方法,方法参数传递Printable接口

public static void pringString(Printable p){

p.print("abcde");

}

public static void main(String[] args) {

//pringString(System.out::print);

//调用printString方法,方法的参数pringable是一个函数式接口,所以可以传递Lambda

pringString((s)->{

//创建MethodRerObject对象

Demo02MethodRerObject methodRerObject=new Demo02MethodRerObject();

//调用Demo02MethodRerObject对象中的成员方法printUpperCaseString,把字符串按照大写输出

methodRerObject.printUpperCaseString(s);

});

/*

使用方法引用优化Lambda

对象已经存在Demo02MethodRerObject

成员方法也是已经存在的printUpperCaseString

所以我们可以使用对象名引用成员方法

*/

Demo02MethodRerObject methodRerObject=new Demo02MethodRerObject();

pringString(methodRerObject::printUpperCaseString);

}

}

Referring to static methods through class names

We have learned before that when there are static methods in our class, we can call the static methods through the class name, and the method reference is also Likewise, static methods can also be referenced through class names. Below we also use code to demonstrate.

This time we define a method. The parameters of the method pass the integer for calculating the absolute value and the functional interface Calcable.

First to define a interface

@FunctionalInterface

public interface Calcable {

//定义一个抽象方法,传递一个整数,对整数进行绝对值计算并返回

int AbsCals(int number);

}

to quote the static member method through the class name, provided that the class already exists, the static member method already exists, and the static member method can be quoted directly through the class name. We also create the class first, define the method, write the code with Lambda, and then use method reference optimization.

public class Demo04StaticMethodReference {

//定义一个方法,方法的参数传递计算绝对值的整数和函数式接口Calcable

public static int method1(int number,Calcable c){

return c.AbsCals(number);

}

public static void main(String[] args) {

//调用method方法,传递计算绝对值的整数和lambda表达式

int number=method1(-10,(n)->{

//对参数进行绝对值计算并返回结果

return Math.abs(n);

});

System.out.println(number);

/*

使用方法引用优化Lambdab表达式

Math类是存在的

abs计算绝对值的静态方法也是存在的

所以我们可以直接通过类名引用静态方法

*/

int number2=method1(-10, Math::abs);

System.out.println(number2);

}

}

Referring to member methods through super

It is mentioned that the super description is related to the parent class method, that is, there is an inheritance relationship. When there is an inheritance relationship and super is called in Lambda, we have a method reference instead.

Define a meeting method

We use the method of meeting and greeting the child and parent classes for demonstration

Similarly this time we define the meeting method Functional interface

/*

定义见面的函数式接口

*/

@FunctionalInterface

public interface Greetable {

//定义一个见面的方法

void greet();

}

Since we need to inherit, we define a parent class

/*

定义父类方法

*/

public class Demo05Fu_Human {

//定义一个sayHello的方法

public void sayHello(){

System.out.println("Hello! 我是Human。");

}

}

        再定义一个子类,在子类中出现父类的成员方法,先使用Lambda编写代码,再进行方法引用优化。

        使用super引用父类的成员方法,前提super是已经存在的,父类的成员方法也是存在的,就可以直接使用super引用父类成员方法。

import java.nio.channels.ShutdownChannelGroupException;

/*

定义子类

*/

public class Demo06Zi_Man extends Demo05Fu_Human {

//子类重写父类sayHello方法

@Override

public void sayHello() {

System.out.println("Hello!我是Man。");

}

//定义一个方法,参数传递Gerrtable接口

public void method(Greetable g){

g.greet();

}

public void show(){

//调用method方法,方法参数Greetable是一个函数式接口,所以可以传递Lambda表达式

method(()->{

//创建父类的Human对象

Demo05Fu_Human fHuman=new Demo05Fu_Human();

fHuman.sayHello();

});

//因为有子父类关系,所以存在的一个关键super,代表父类,可以直接使用super调用父类的成员方法

method(()->{

super.sayHello();

});

/*

使用super引用类的成员方法

super是已经存在的

父类的成员方法也是存在的

使用可以直接使用super引用父类成员方法

*/

method(super::sayHello);

}

public static void main(String[] args) {

//调用show方法

new Demo06Zi_Man().show();

}

}

通过this引用成员方法

        既然上面用super引用了父类的成员方法,我们之前也学过this也可以调用本类的成员方法,那同样this也可以引用本类的成员方法。

示例:

定义一个买房子的方法

同样,首先定义函数式接口。

/*

定义一个富有的函数式接口

*/

@FunctionalInterface

public interface Richable {

//定义一个想买什么就买什么的方法

void buy();

}

        然后怎么创建类,再定义买房子的方法。通过this引用成员方法,前提this是已经存在的,买房子的成员方法也是存在的,就可以直接使用this引用成员方法。同样先使用Lambda编写代码,再进行方法引用优化。

/*

通过this引用本类的成员方法

*/

public class Demo07_Husband {

//定义一个买房子的方法

public void buyHouse(){

System.out.println("北京二环内买一套四合院!");

}

//定义一个结婚的方法,参数传递Richable接口

public void marry(Richable r){

r.buy();

}

//定义一个高兴的方法

public void soHappy(){

//调用结婚的方法,方法的参数Richable是一个函数式接口,传递Lambda表达式

marry(()->{

//使用this,成员方法,调用本类买房子的方法

this.buyHouse();

});

/*

使用方法引用优化Lambda

this是已经存在的

买房子的成员方法也是存在的

可以直接使用this引用成员方法

*/

marry(this::buyHouse);

}

public static void main(String[] args) {

new Demo07_Husband().soHappy();

}

}

类的构造器引用

        类的构造器引用也叫构造方法引用。而由于构造器名称和类名完全一样,所以构造器引用格式是这样的,类名称::new的格式表示。既然是构造器引用也就是构造方法引用,所以我们需要:

定义一个Person类。

/*

person类

*/

public class Person {

private String name;

public Person() {

super();

// TODO Auto-generated constructor stub

}

public Person(String name) {

super();

this.name = name;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

然后创建一个Person对象的函数式接口

*

定义一个创建erson对象的函数式接口

*/

@FunctionalInterface

public interface PersonBuilder {

//定义一个方法,根据传递的姓名,创建person对象返回

Person buliderPerson(String name);

}

        再传递一个方法,参数传递姓名和PersonBulider接口,方法中通过 姓名创建Person对象。类的构造器引用,前提构造方法new Person(String name)已知,创建对象已知 new,就可以使用Person引用new创建对象。同样先使用Lambda编写代码,再进行方法引用优化。

/*

类的构造器(构造方法)引用

*/

import java.time.chrono.MinguoChronology;

import javax.print.attribute.standard.PrinterName;

public class Demo08Person {

//传递一个方法,参数传递姓名和PersonBulider接口,方法中通过 姓名创建Person对象

public static void printName(String name,PersonBuilder pb){

Person person=pb.buliderPerson(name);

System.out.println(person.getName());

}

public static void main(String[] args) {

//调用printName方法,方法的参数传递了函数式接口,我们可以使用Lambda表达式

printName("张三",(name)->{

return new Person(name);

});

/*使用方法引用优化Lambda表达式

构造方法new Person(String name)已知

创建对象已知 new

就可以使用Person引用new创建对象*/

printName("痛而不言笑而不语的浅伤",Person::new);

}

}

数组的构造器引用

        数组也是Object的子类,所以它也有方法引用,只是语法上稍有不同。

示例:

定义一个方法

方法的参数传递创建数组的长度和ArrayBulider接口

方法内部根据创建的长度使用ArrayBuilder中的方法创建数组并返回

同样,先创建一个数组的函数式接口

/*

定义一个创建数组的函数式接口

*/

@FunctionalInterface

public interface ArrayBulider {

// 定义一个int类型的数组方法,参数传递数组的长度,返回创建好的int类型的数组

int[] buliderArray(int length);

}

        方法的参数传递创建数组的长度和ArrayBulider接口,方法内部根据创建的长度使用ArrayBuilder中的方法创建数组并返回。前提,已知创建的就是int[]数组,数组的长度也是已知的,就可以通过数组int[]引用new,根据参数传递的长度来创建数组同样先使用Lambda编写代码,再进行方法引用优化。

import java.lang.reflect.Array;

import java.util.Arrays;

/*

数组的构造器引用

*/

public class Demo09Array_BuilderArray {

/*
定义一个方法

方法的参数传递创建数组的长度和ArrayBulider接口

方法内部根据创建的长度使用ArrayBuilder中的方法创建数组并返回

*/

public static int[] arrayLength(int length,ArrayBulider ab){

return ab.buliderArray(length);

}

public static void main(String[] args) {

//调用arrayLength方法、传递数组的长度和Lambda表达式

int[]arr=arrayLength(10,(len)->{

return new int[len];

});

System.out.println(arr.length);

/*使用方法引用优化Lambda表达式

已知创建的就是int[]数组

数组的长度也是已知的

就可以通过数组int[]引用new,根据参数传递的长度来创建数组*/

int[]arr1=arrayLength(5, int[]::new);

System.out.println(arr1.length);

System.out.println(Arrays.toString(arr1));

}

}

推荐学习:《java视频教程

The above is the detailed content of An in-depth analysis of method references in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete