java第三个属性值等于第一个属性值与一个字符串拼接

public class UserTest {
}
class User{
    private String id;
    private String password;
    private String email;//目标:缺省的email地址是id加上字符串"@email.com"
}

无法直接在属性定义处拼接
可以在get方法中实现拼接

    public String getEmail() {
        return id + email;
    }

完整代码

public class UserTest {
    public static void main(String[] args) {
        User user = new User("111111", "123456");
        System.out.println(user.getEmail());
    }
}
class User{
    private String id;
    private String password;
    private String email;//目标:缺省的email地址是id加上字符串"@email.com"

    public User() {
    }

    public User(String id, String password) {
        this.id = id;
        this.password = password;
    }

    public User(String id, String password, String email) {
        this.id = id;
        this.password = password;
        this.email = email;
    }

    public String getId() {
        return id;
    }

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

    public String getPassword() {
        return password;
    }

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

    public String getEmail() {
        return id + "@email.com";
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

运行结果
java第三个属性值等于第一个属性值与一个字符串拼接_第1张图片

你可能感兴趣的:(java,java,开发语言)