当前位置: 首页 > 图灵资讯 > 技术篇> Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)

Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)

来源:图灵教育
时间:2023-06-17 13:58:02

一键生成标准Javabean(一键生成结构方法和Setter和Getter方法)方法1:快捷键

快捷键:  alt+insert  或alt+fn+insert

1.生成参与和无参构造函数

Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)_右键

Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)_User_02

public User() {    }    public User(String username, String password, String cfpassword, String email, String gender, int age) {        this.username = username;        this.password = password;        this.cfpassword = cfpassword;        this.email = email;        this.gender = gender;        this.age = age;    }

2.生成Getter和Setter方法

Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)_右键_03

Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)_右键_04

 

public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getCfpassword() {        return cfpassword;    }    public void setCfpassword(String cfpassword) {        this.cfpassword = cfpassword;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public String getGender() {        return gender;    }    public void setGender(String gender) {        this.gender = gender;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }

3.生成tostring方法

Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)_右键_05

Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)_User_06

 

@Override    public String toString() {        return "User{" +                "username='" + username + '\'' +                ", password='" + password + '\'' +                ", cfpassword='" + cfpassword + '\'' +                ", email='" + email + '\'' +                ", gender='" + gender + '\'' +                ", age=" + age +                '}';    }

 

方法2:JavaBeann标准插件-1s生成标准

1.安装插件

Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)_右键_07

 

2.右键-使用插件

Idea小技巧一键生成标准JavaBean(一键生成构造方法和Setter和Getter方法)_User_09

 

public User() {}public User(String username, String password, String cfpassword, String email, String gender, int age) {    this.username = username;    this.password = password;    this.cfpassword = cfpassword;    this.email = email;    this.gender = gender;    this.age = age;}/** * 获取 * @return username */public String getUsername() {    return username;}/** * 设置 * @param username */public void setUsername(String username) {    this.username = username;}/** * 获取 * @return password */public String getPassword() {    return password;}/** * 设置 * @param password */public void setPassword(String password) {    this.password = password;}/** * 获取 * @return cfpassword */public String getCfpassword() {    return cfpassword;}/** * 设置 * @param cfpassword */public void setCfpassword(String cfpassword) {    this.cfpassword = cfpassword;}/** * 获取 * @return email */public String getEmail() {    return email;}/** * 设置 * @param email */public void setEmail(String email) {    this.email = email;}/** * 获取 * @return gender */public String getGender() {    return gender;}/** * 设置 * @param gender */public void setGender(String gender) {    this.gender = gender;}/** * 获取 * @return age */public int getAge() {    return age;}/** * 设置 * @param age */public void setAge(int age) {    this.age = age;}public String toString() {    return "User{username = " + username + ", password = " + password + ", cfpassword = " + cfpassword + ", email = " + email + ", gender = " + gender + ", age = " + age + "}";}