您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么深入理解java1.8知道supplier,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
supplier也是是用來創(chuàng)建對象的,但是不同于傳統(tǒng)的創(chuàng)建對象語法:new,看下面代碼:
public class TestSupplier { private int age;
TestSupplier(){ System.out.println(age);
} public static void main(String[] args) {
//創(chuàng)建Supplier容器,聲明為TestSupplier類型,此時并不會調(diào)用對象的構(gòu)造方法,即不會創(chuàng)建對象 Supplier<TestSupplier> sup= TestSupplier::new;
System.out.println("--------");
//調(diào)用get()方法,此時會調(diào)用對象的構(gòu)造方法,即獲得到真正對象 sup.get();
//每次get都會調(diào)用構(gòu)造方法,即獲取的對象不同 sup.get();
}}
輸出結(jié)果:
--------00
官方代碼及注釋:
/** * Represents a supplier of results. *
* <p>There is no requirement that a new or distinct result be returned each * time the supplier is invoked. *
* <p>This is a <a href="package-summary.html" rel="external nofollow" >functional interface</a> *
whose functional method is {@link #get()}.
* * @param <T> the type of results supplied by this supplier
* * @since 1.8 */@FunctionalInterfacepublic interface Supplier<T> {
/** * Gets a result. *
* @return a result */ T get();
}
根據(jù)代碼和官方注釋,我的個人理解:
1.supplier是個接口,有一個get()方法
2.語法 :
Supplier<TestSupplier> sup= TestSupplier::new;
3.每次調(diào)用get()方法時都會調(diào)用構(gòu)造方法創(chuàng)建一個新對象。
上述內(nèi)容就是怎么深入理解java1.8知道supplier,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。