Lambda表达式和Lombok

  • Lambda
一、如何用Lambda表达式写?
        (形参列表) -> {方法体的落地实现}
二、要求
        接口有且只能有一个方法,新的注解标识要求@FunctionalInterface
例:
@FunctionalInterface
interface Foo
{
    public int add(int x,int y);
    
    default int div(int x,int y)
    {
        return x / y;
    }
    
    default int div2(int x,int y)
    {
        return x / y;
    }
    
    public static int div3(int x,int y)
    {
        return x / y;
    }
}


public class LambdaDemo
{
    public static void main(String[] args)
    {
//        Foo foo = new Foo() {
//            @Override
//            public void sayHello()
//            {
//                System.out.println("******hello 0725");
//            }
//        };
//        foo.sayHello();
        
        
        
        Foo foo = (x,y) -> { return x + y;};
        int result = foo.add(2, 5);
        System.out.println("********result:  "+result);
        
        result = foo.div(10, 2);
        System.out.println("********result:  "+result);
        
        System.out.println(Foo.div3(20, 4));
        
    }
}

  • Lombok
步骤一:lambok.jar放入D盘并打开cmd窗口运行
            java -jar lombok.jar
步骤二:会出来一个安装窗口选中指定eclipse的安装路径
步骤三:修改pom配置
         < dependency >
      < groupId > org.projectlombok </ groupId >
      < artifactId > lombok </ artifactId >
      < version > 1.16.6 </ version >
      < scope > provided </ scope >
    </ dependency >
步骤四:加上注解
package test;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor   //这个是全参构造器
@NoArgsConstructor  //这个是无参构造器
@Data  //这个是getter和setter还有toString
public class Book {
private int id;
private String name;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值