private boolean isfromself; public boolean isIsfromself() { return isfromself; } public void setIsfromself(boolean isfromself) { this.isfromself = isfromself; } if(user_id==user){ userMsg.setIsfromself(true); }else{ userMsg.setIsfromself(false); }优化代码
时间: 2023-06-14 12:03:32 浏览: 100
优化代码可以使用三元表达式来简化if-else语句,如下所示:
```
userMsg.setIsfromself(user_id == user ? true : false);
```
也可以直接将判断语句的结果赋值给isfromself变量,如下所示:
```
userMsg.setIsfromself(user_id == user);
```
这样可以减少代码行数并提高代码可读性。
相关问题
class Gezi { private int x; private int y; private boolean filled; private boolean searched; private boolean colored; private boolean go; private boolean start; private boolean end; public boolean isStart() { return start; } public void setStart(boolean start) { this.start = start; } public boolean isEnd() { return end; } public void setEnd(boolean end) { this.end = end; } public boolean isGo() { return go; } public void setGo(boolean go) { this.go = go; } public boolean isColored() { return colored; } public void setColored(boolean colored) { this.colored = colored; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public boolean isFilled() { return filled; } public void setFilled(boolean filled) { this.filled = filled; } public boolean isSearched() { return searched; } public void setSearched(boolean searched) { this.searched = searched; } public Gezi() { } public Gezi(int x,int y,boolean filled,boolean searched) { this.filled = filled; this.x = x; this.y = y; this.searched = searched; } }
这是一个Gezi类,它定义了8个私有属性和8个公共方法。其中,x和y表示该Gezi对象在平面上的位置,filled和colored是布尔类型的属性,表示该Gezi对象是否被填充和是否被染色,searched和go也是布尔类型的属性,表示该Gezi对象是否被访问过和是否可以通过。start和end也是布尔类型的属性,表示该Gezi对象是否为起点和终点。这个类可以用于表示平面上的一个格子,并记录格子是否被填充、染色、访问过,以及是否可以通过和是否为起点或终点。
public class EncryptConfig { private Boolean enable; private String key; private String iv; private String salt; public EncryptConfig(Boolean enable, String key, String iv, String salt) { this.enable = enable; this.key = key; this.iv = iv; this.salt = salt; } public boolean getEnable() { return enable; } public void setEnable(boolean enable) { this.enable = enable; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getIv() { return iv; } public void setIv(String iv) { this.iv = iv; } public String getSalt() { return salt; } public void setSalt(String salt) { this.salt = salt; } } 根据上述类写出可以通过build方法构造实例的代码
由于该类已经提供了带参数的构造函数和getter/setter方法,因此可以直接使用构造函数和setter方法来构造实例,不需要再写一个builder类。
以下是可以通过构造函数和setter方法构造EncryptConfig实例的代码:
```
EncryptConfig config = new EncryptConfig(true, "my key", "my iv", "my salt");
config.setEnable(true);
config.setKey("my key");
config.setIv("my iv");
config.setSalt("my salt");
```
阅读全文