java——除了new,你还会用supplier创建对象吗?

作者专注于Java、架构、Linux、小程序、爬虫、自动化等技术。 工作期间含泪整理出一些资料,微信搜索【程序员高手之路】,回复 【java】【黑客】【爬虫】【小程序】【面试】等关键字免费获取资料。 

一、官方给的接口

使用FunctionalInterface注解修饰接口,只有一个get方法

@FunctionalInterface
public interface Supplier<T> {

    /**
     * Gets a result.
     *
     * @return a result
     */
    T get();
}

二、解析

如下列代码所示:

使用Supplier创建对象,语法结构:

无参数:

1. Supplier<T> instance = T::new;

2. Supplier<T> instance = () -> new T();

有参数:

1. Function<String, T> fun = T::new;
    fun.apply("test");

2. Function<String, T> fun2 = str -> new T(str);
    fun2.apply("test2");

注:每次调用get方法都会创建一个对象,下面的代码中调用了两次get方法,打印的hashcode是不一样的!

public class TestSupplier {
	public static void main(String[] args) {
		//无参数1:
		Supplier<TestSupplier> sup = TestSupplier::new;
		sup.get();
		sup.get();
		//无参数2:
		Supplier<TestSupplier> sup2 = () -> new TestSupplier();
		sup2.get();
		sup2.get();
		
		//有参数1:
		Function<String, TestSupplier> fun = TestSupplier::new;
		fun.apply("test");
		//有参数2:
		Function<String, TestSupplier> fun2 = str -> new TestSupplier(str);
		fun2.apply("test2");
	}

	public TestSupplier() {
		System.out.println(this.hashCode());
	}
	
	public TestSupplier(String str) {
		System.out.println(this.hashCode() + ",参数:" + str);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前方一片光明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值