lamda里的s是代表accept方法里的参数text吗?

lamda里的s是代表accept方法里的参数text吗?

那这里只是演示了一个参数打印的意思,conusmer.accept(text)这个无返回值的方法里面具体是怎么运行的呢?就是如果传的argument是String,原样输出吗?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
好帮手慕小小 2022-03-16 18:37:38

同学你好,并不是哦,accept方法是一个有参数无输出的功能代码,具体的代码实现由实现类定义。

https://img1.sycdn.imooc.com//climg/6231bd9e090ca3a311360148.jpg

祝学习愉快~

好帮手慕小脸 2022-03-16 18:37:35

同学你好,

1、这里的s是accept方法的参数,“System.out.println("向控制台打印"+s)”是accept方法的具体实现。

https://img1.sycdn.imooc.com//climg/6231bd8909dae50110720217.jpg

2、建议同学选择代码语言后再将代码进行反馈,老师这边显示内容是乱码呢

祝学习愉快~



  • 提问者 晓舟 #1
    /*源码里在调用accept方法后 如何看出它只会执行->之后的句子呢?
    /如何从源码里看出Consumer方法的实现呢?
      default Consumer<T> andThen(Consumer<? super T> after) {
            Objects.requireNonNull(after);
            return (T t) -> { accept(t); after.accept(t); };
            
     * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
     * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     *
     */
    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">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<? super T> after) {
            Objects.requireNonNull(after);
            return (T t) -> { accept(t); after.accept(t); };
        }
    }


    2022-03-18 10:16:29
  • 好帮手慕小脸 回复 提问者 晓舟 #2

    同学你好, 

    1、调用output方法,该方法的参数是Consumer接口,该是一个函数式接口,所以可以传递Lambda表达式,这里也可以打断点依次执行

    注:lambda表达式的语法格式
    函数式接口中的方法无参数   () ->{//do something}
    如果只有一个参数 x ->{//do something}

    2、Consumer接口是一个消费型接口,泛型指定什么类型,就可以使用accept方法消费什么类型的数据

    祝学习愉快~

    2022-03-18 14:12:46
晓舟 提问者 2022-03-16 18:31:59


/*源码里在调用accept方法后 如何看出它只会执行->之后的句子呢?

 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.

 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 *

 */

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">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<? super T> after) {

        Objects.requireNonNull(after);

        return (T t) -> { accept(t); after.accept(t); };

    }

}


晓舟 提问者 2022-03-16 18:30:23
java.util.function;

java.util.Objects;

Consumer<> {

    accept(t);

    Consumer<> andThen(Consumer<? > after) {
        Objects.(after);
        (t) -> { accept(t); .accept(t); };
    }
}


问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师