构造方法直接赋值之后,set方法还需要定义吗,是为了方便继续使用还是有其必要性

Anacon 2018-04-16 08:18:10
package cn.campsg.gm.entity;

public class User {

private String id = null;
private String name = null;
private String password = null;
private Byte sex = null;
private String city = null;
private ShoppingCart cart = null;

public User() {
cart = new ShoppingCart();
}

public User(String id, String name, String password, Byte sex,
String city) {
this();
this.id = id;
this.name = name;
this.password = password;
this.sex = sex;
this.city = city;
}

public ShoppingCart getCart() {
return cart;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public Byte getSex() {
return sex;
}

public void setSex(Byte sex) {
this.sex = sex;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

}

...全文
1317 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ooo-ooo 2018-04-21
  • 打赏
  • 举报
回复
如果能保证每次创建对象都是使用含参构造方法,setter就不用了
  • 打赏
  • 举报
回复
构造方法是为了创建对象时,传入一些必要的参数用来初始化对象。 setter/getter是为了控制属性可不可以读写 两者不矛盾
weixin_42007791 2018-04-17
  • 打赏
  • 举报
回复
创建对象后是在测试类中使用的,直接赋值的话已经没有对象的这个意义了;
Bestprotect 2018-04-17
  • 打赏
  • 举报
回复
构造函数是方便赋值 不用new完在set了 set get是为了后面对这个对象进行其他操作
卡布奇诺er 2018-04-17
  • 打赏
  • 举报
回复
创建对象后,如果没有其他更新操作的话,可以不要setter
Braska 2018-04-17
  • 打赏
  • 举报
回复
看需要啊,如果这个属性只读的话就没必要写setter了。
maradona1984 2018-04-17
  • 打赏
  • 举报
回复
这个看你具体需求,一般情况下还是有存在的必要性的

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧