#Lombok的简单使用 @FDDLC
发布日期:2021-06-30 21:04:47 浏览次数:3 分类:技术文章

本文共 3118 字,大约阅读时间需要 10 分钟。

话题引入:曾几何时,我们一次又一次地为getter、setter、toString等做着机械而无意义的重复。自从有了Lombok,我们被解放了。

 

一、准备工作

本人使用的是最新版的IDEA(2021.1),IDEA自身已安装Lombok插件(不是我安装的):

故只需引入Lombok依赖即可:

 

 

二、使用

1、User.java:

package cn.fddlc;import lombok.AllArgsConstructor;import lombok.Data;@Data@AllArgsConstructor//全参构造(按书写顺序)public class User {    private String username;    private String password;}

 

2、Main.java:

package cn.fddlc;public class Main {    public static void main(String[] args) {        User user=new User("张三","123456");//@AllArgsConstructor        System.out.println("toString:"+user);//@Data:toString        System.out.println("getter:"+user.getUsername());//@Data:getter        User user2=new User("张三","123456");        System.out.println("hashCode:"+user.hashCode()+","+user2.hashCode());//@Data:hashCode        System.out.println("equals:"+user.equals(user2));//@Data:equals        user.setPassword("密码改了……");//@Data:setter        System.out.println("setter之后再getter:"+user.getPassword());    }}

 

三、输出

 

四、用法小结

1、@Getter:可以作用在整个类上,使类中的非static属性拥有getter;也可以作用在单个属性上,被注解的属性拥有getter。

2、@Setter:可以作用在整个类上,使类中的非static属性拥有setter;也可以作用在单个属性上,被注解的属性拥有setter。

3、@RequiredArgsConstructor:使用被final或@NonNull等修饰的属性来构造对象

4、@ToString:

5、EqualsAndHashCode:

6、@Data:相当于@Getter、@Setter、@RequiredArgsConstructor、@ToString、@EqualsAndHashCode

 

 

五、Delombok:去Lombok化,把Lombok注解换成等效的普通代码

说明:Delombok可以针对单个文件,也可以针对整个包或目录;可以针对单个Lombok注解(比如@Data),也可以针对所有Lombok注解。

以Delombok单个文件为例:

1、Delombok前:

package cn.fddlc;import lombok.Data;@Datapublic class Account {    private Integer id;    private Double money;}

2、Delombok:

3、Delombok后:

package cn.fddlc;public class Account {    private Integer id;    private Double money;    public Account() {    }    public Integer getId() {        return this.id;    }    public Double getMoney() {        return this.money;    }    public void setId(Integer id) {        this.id = id;    }    public void setMoney(Double money) {        this.money = money;    }    public boolean equals(final Object o) {        if (o == this) return true;        if (!(o instanceof Account)) return false;        final Account other = (Account) o;        if (!other.canEqual((Object) this)) return false;        final Object this$id = this.getId();        final Object other$id = other.getId();        if (this$id == null ? other$id != null : !this$id.equals(other$id)) return false;        final Object this$money = this.getMoney();        final Object other$money = other.getMoney();        if (this$money == null ? other$money != null : !this$money.equals(other$money)) return false;        return true;    }    protected boolean canEqual(final Object other) {        return other instanceof Account;    }    public int hashCode() {        final int PRIME = 59;        int result = 1;        final Object $id = this.getId();        result = result * PRIME + ($id == null ? 43 : $id.hashCode());        final Object $money = this.getMoney();        result = result * PRIME + ($money == null ? 43 : $money.hashCode());        return result;    }    public String toString() {        return "Account(id=" + this.getId() + ", money=" + this.getMoney() + ")";    }}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载地址:https://liuxingchang.blog.csdn.net/article/details/116268704 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:#力扣 LeetCode剑指 Offer 68 - I. 二叉搜索树的最近公共祖先 @FDDLC
下一篇:#力扣 LeetCode1672. 最富有客户的资产总量 @FDDLC

发表评论

最新留言

关注你微信了!
[***.104.42.241]2024年04月26日 19时30分34秒