暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Java新特性解读JDK8之函数式接口Supplier

227decision 2020-01-17
1850
Supplier作为生产型接口:无入参,有返回值

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">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使用

      package com.example.functionalinterfacedemo;
      import java.util.function.Supplier;
      public class SupplierDemo {
      public static void main(String[] args) {
      getObj();
      }
      /**
      * 可用于工厂设计模式,无参的工厂方法
      * @return
      */
      public static Object getObj() {
      Supplier<Object>supplier=()->{
      System.out.println("生产对象");
            return new Object();
      };
      return supplier.get();
        }
      }


      最后修改时间:2020-07-01 13:37:30
      文章转载自227decision,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

      评论