双冒号作用:
jdk1.8中lambda 经常需要操作函数式接口表达式,表达式也是需要人写出来的,如果有现成的,那么久不需要写了,这里的双冒号其实就是对方法体的一种引用。
以下是Java 8中方法引用的一些语法:
1、静态方法引用(static method)语法:classname::methodname 例如:Person::getAge
2、对象的实例方法引用语法:instancename::methodname 例如:System.out::println
3、对象的超类方法引用语法: super::methodname
4、类构造器引用语法: classname::new 例如:ArrayList::new
5、数组构造器引用语法: typename[]::new 例如: String[]:new
静态方法语法示例:
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* @author wmd
* @date 2019年2月20日 下午2:19:13
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("com.zhoufy")
public class Demo {
@Test
public void test() {
List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");
//静态方法语法 ClassName::methodName
list.forEach(Demo::print);
}
public static void print(String content){
System.out.println(content);
}
}
类实例方法语法使用示例:
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* @author wmd
* @date 2019年2月20日 下午2:19:13
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("com.zhoufy")
public class Demo {
@Test
public void test() {
List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");
//对象实例语法 instanceRef::methodName
list.forEach(new Demo()::print);
}
public void print(String content){
System.out.println(content);
}
}
超类方法语法使用示例:
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author wmd
* @date 2019年2月20日 下午2:41:38
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("com.zhoufy")
public class Example extends BaseExample{
@Test
public void test() {
List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");
//对象的超类方法语法: super::methodName
list.forEach(super::print);
}
}
class BaseExample {
public void print(String content){
System.out.println(content);
}
}
类构造器语法使用示例:某个接口的内部类是个表达式,表达式借用对象的无惨构造器来完成。
无惨构造:
public class Example {
public Example() {
}
@Test
public void test() {
Runnable aNew = Example::new;//利用Example无惨构造创造的内部类,表达式就是构造方法
InterfaceExample com = Example::new;//利用Example无惨构造创造的内部类,表达式就是构造方法
InterfaceExample com2 = Example::new;//利用Example无惨构造创造的内部类,表达式就是构造方法
Example bean = com.create();
System.out.println(bean);
}
interface InterfaceExample{
Example create();
}
}
有参构造:
public class Example {
private String name;
Example(String name){
this.name = name;
}
@Test
public void test() {
InterfaceExample com = Example::new;//利用Example无惨构造创造的内部类,表达式就是构造方法
InterfaceExample com3 = (str)->{return new Example(str);};
//因为com3是InterfaceExample的实现类啊,它是可以简化成上面这样,其实它就是返回一个对象,所以它等于Example的有参构造
Example bean = com.create("用的是Example的有参构造器当表达式");
System.out.println(bean);
}
interface InterfaceExample{
Example create(String name);
}
}
数组构造器语法使用例子:
package top.lisicheng;
import java.util.function.Function;
/**
* @ClassNameExampl
* @Description:
* @Author:wumingdu
* @Date2022/4/3 17:17
* @Version V1.0
**/
public class Exampl {
//数组构造器
public Exampl[] Exampl(Integer integer) {
Exampl[] qqq = new Exampl[integer];
return qqq;
}
public static void main(String[] args) {
Function<Integer, Exampl[]> integerFunction1 = new Function<Integer, Exampl[]>() {
@Override
public Exampl[] apply(Integer integer) {
return new Exampl[integer];
}
};
Function<Integer, Exampl[]> integerFunction2 = Exampl[]::new;
//integerFunction2和integerFunction1的作用是一样的,
Exampl[] array = integerFunction2.apply(4); //这里的4是数组的大小
for(Exampl e:array){
System.out.println(e); //如果输出的话,你会发现会输出4个空对象(null)
}
}
}