Java - Boolean & boolean 定义 POJO 的区别?

【强制】velocity 调用 POJO 类的属性时,建议直接使用属性名取值即可,模板引擎会自动按规范调用 POJO 的 getXxx(),如果是 boolean 基本数据类型变量(boolean 命名不需要加 is 前缀),会自动调用 isXxx()方法。

说明:注意如果是 Boolean 包装类对象,优先调用 getXxx()的方法。

Java 文件 

package tech.luxsun.interview.luxinterviewstarter.datatype;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Lux Sun
 * @date 2021/5/6
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BooleanDemo0 {

    private Boolean isActive;

    private boolean isDel;
}

Class 文件

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package tech.luxsun.interview.luxinterviewstarter.datatype;

public class BooleanDemo0 {
    private Boolean isActive;
    private boolean isDel;

    public Boolean getIsActive() {
        return this.isActive;
    }

    public boolean isDel() {
        return this.isDel;
    }

    public void setIsActive(final Boolean isActive) {
        this.isActive = isActive;
    }

    public void setDel(final boolean isDel) {
        this.isDel = isDel;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof BooleanDemo0)) {
            return false;
        } else {
            BooleanDemo0 other = (BooleanDemo0)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.isDel() != other.isDel()) {
                return false;
            } else {
                Object this$isActive = this.getIsActive();
                Object other$isActive = other.getIsActive();
                if (this$isActive == null) {
                    if (other$isActive != null) {
                        return false;
                    }
                } else if (!this$isActive.equals(other$isActive)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof BooleanDemo0;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        int result = result * 59 + (this.isDel() ? 79 : 97);
        Object $isActive = this.getIsActive();
        result = result * 59 + ($isActive == null ? 43 : $isActive.hashCode());
        return result;
    }

    public String toString() {
        return "BooleanDemo0(isActive=" + this.getIsActive() + ", isDel=" + this.isDel() + ")";
    }

    public BooleanDemo0() {
    }

    public BooleanDemo0(final Boolean isActive, final boolean isDel) {
        this.isActive = isActive;
        this.isDel = isDel;
    }
}

总结

  • 根据以上 Alibaba Java 规范,我特地试了下两者的区别,最后发现推荐使用 Boolean 来定义属性,这样就可以使用 isXXX 命名,可读性也比较好~
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放羊的牧码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值