Java - Boolean & boolean 定义 POJO 的区别?
Posted Lux_Sun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 命名,可读性也比较好~
以上是关于Java - Boolean & boolean 定义 POJO 的区别?的主要内容,如果未能解决你的问题,请参考以下文章