bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

Java8Supplier接口和Consumer接口的用法

這篇文章主要講解了Java8 Supplier接口和Consumer接口的用法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

創新互聯建站于2013年開始,我們提供高端網站建設、成都小程序開發、電商視覺設計、APP應用開發及網絡營銷搜索優化服務,在傳統互聯網與移動互聯網發展的背景下,我們堅守著用標準的設計方案與技術開發實力作基礎,以企業及品牌的互聯網商業目標為核心,為客戶打造具商業價值與用戶體驗的互聯網+產品。

Supplier接口

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.
 *
 * <p>This is a <a href="package-summary.html" rel="external nofollow" 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
 */
@FunctionalInterface
public interface Supplier<T> {
  /**
   * Gets a result.
   *
   * @return a result
   */
  T get();
}

supplier接口只有一個抽象方法get(),通過get方法產生一個T類型實例。

實例:

package me.yanand;
import java.util.function.Supplier;
public class TestSupplier {
  public static void main(String[] args) {
    Supplier<Apple> appleSupplier = Apple::new;
    System.out.println("--------");
    appleSupplier.get();
  }
}
class Apple{
  public Apple() {
    System.out.println("創建實例");
  }
}

Consumer接口

package java.util.function;
import java.util.Objects;
/**
 * Represents an operation that accepts a single input argument and returns no
 * result. Unlike most other functional interfaces, {@code Consumer} is expected
 * to operate via side-effects.
 *
 * <p>This is a <a href="package-summary.html" rel="external nofollow" rel="external nofollow" >functional interface</a>
 * whose functional method is {@link #accept(Object)}.
 *
 * @param <T> the type of the input to the operation
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Consumer<T> {
  /**
   * Performs this operation on the given argument.
   *
   * @param t the input argument
   */
  void accept(T t);
  /**
   * Returns a composed {@code Consumer} that performs, in sequence, this
   * operation followed by the {@code after} operation. If performing either
   * operation throws an exception, it is relayed to the caller of the
   * composed operation. If performing this operation throws an exception,
   * the {@code after} operation will not be performed.
   *
   * @param after the operation to perform after this operation
   * @return a composed {@code Consumer} that performs in sequence this
   * operation followed by the {@code after} operation
   * @throws NullPointerException if {@code after} is null
   */
  default Consumer<T> andThen(Consumer<&#63; super T> after) {
    Objects.requireNonNull(after);
    return (T t) -> { accept(t); after.accept(t); };
  }
}

一個抽象方法accept(T t)定義了要執行的具體操作;注意看andThen方法,接收Consumer<&#63; super T>類型參數,返回一個lambda表達式,此表達式定義了新的執行過程,先執行當前Consumer實例的accept方法,再執行入參傳進來的Consumer實例的accept方法,這兩個accept方法接收都是相同的入參t。

實例:

package me.yanand;
import java.util.function.Consumer;
public class TestConsumer {
  public static void main(String[] args) {
    Consumer<Integer> consumer = (t) -> {
      System.out.println(t*3);
    };
    Consumer<Integer> consumerAfter = (s) -> {
      System.out.println("之后執行:"+s);
    };
    consumer.andThen(consumerAfter).accept(5);
  }
}

看完上述內容,是不是對Java8 Supplier接口和Consumer接口的用法有進一步的了解,如果還想學習更多內容,歡迎關注創新互聯行業資訊頻道。

網站標題:Java8Supplier接口和Consumer接口的用法
分享地址:http://vcdvsql.cn/article18/gghpdp.html

成都網站建設公司_創新互聯,為您提供外貿網站建設動態網站品牌網站設計ChatGPT服務器托管網頁設計公司

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都網站建設公司