Java 8 新特性:5-Supplier、IntSupplier、BinaryOperator接口
阿新 • • 發佈:2017-07-01
point except java 8 htm import void int() uci cti
/* */ #get()}.
*這是一個方法為get的函數式接口
* @param <T> the type of results supplied by this supplier
*
* @since 1.8
*/
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
(原)
這個接口很簡單,裏面只有一個抽象方法,沒有default和靜態方法。
/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util.function; /** * Represents a supplier of results. *代表了一個供應的結果 * <p>There is no requirement that a new or distinct result be returned each * time the supplier is invoked. * 沒有要求supplier 被調用以後都需要返回一個新的或者不同的結果 * <p>This is a <a href="package-summary.html">functional interface</a> * whose functional method is [email protected]
如果前面幾篇看過了,那麽這個會很簡單。不接收參數,返回的類型隨調用者控制,這個有點類似於不接收參數的工廠模式。
然後有一個與之相關的,返回int類型的函數式接口,叫IntSupplier,
它裏面的方法是這樣的。
/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util.function; /** * Represents a supplier of [email protected] int}-valued results. This is the * [email protected]