lambda表达式快速入门

lambda表达式快速入门

@FunctionalInterface
interface Foo
{
    //public void sayHello();
    public int add(int x, int y);


    /*
        这里default只能有一个
     */
    default int mul(int x, int y)
    {
        return x * y;
    }

    public static int div(int x, int y)
    {
        return x/y;
    }



}


/**
 * @auther zzyy
 *
 * 1    拷贝小括号, 写死右箭头, 落地大括号
 * 2    @FunctionalInterface
 * 3    default
 * 4    static
 */
public class LambdaExpressDemo02
{
    public static void main(String[] args)
    {
        /*Foo foo = new Foo()
        {
            @Override
            public void sayHello()
            {
                System.out.println("****hello 1205");
            }

            @Override
            public int add(int x, int y)
            {
                return 0;
            }
        };
        foo.sayHello();*/

        Foo foo = (int x,int y) -> {
            System.out.println("come in add method");
            return x + y;
        };
        System.out.println(foo.add(3, 5));

        System.out.println(foo.mul(3, 5));

        System.out.println(Foo.div(10, 2));

    }
}

发布了38 篇原创文章 · 获赞 1 · 访问量 2273

猜你喜欢

转载自blog.csdn.net/CRD8843/article/details/103595266
今日推荐