首页 > 解决方案 > 即使使用 Long 类型,整数也太大

问题描述

我在使用 Long 类型属性时遇到问题,我创建了一个名为“Cnh”的属性,它接收一个 11 位代码,我已经将 L 放在了值的末尾,但是当我在 JUnit 中运行它时,它仍然显示 Integer number大号:06161567318l。

有什么可能的方法可以避免它我不想更改属性类型,因为我有另一个有同样问题的但它是一个“哈希码”(这真的很难改变)

设置值

public void setUp() {
    cli = new Cliente();
    cli.setEmail("lucasoaresleite@gmail.com");
    cli.setCpf(06161567318l);
    cli.setCnh(06161567318l);
    cli.setCnpj("--");
    cli.setDataNascimento("23081998");
    cli.setNomeCompleto("Lucas Soares");
    cli.setNomeFantasia("--");
}

JUnit 测试

public void testSetCnh() {
    assertEquals(06161567318l, cli.getCnh());
}

公共类 Cliente 实现 Serializable {

private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "cpf")
private Long cpf;
@Basic(optional = false)
@Column(name = "nome_completo")
private String nomeCompleto;
@Basic(optional = false)
@Column(name = "data_nascimento")
private String dataNascimento;
@Basic(optional = false)
@Column(name = "email")
private String email;
@Basic(optional = false)
@Column(name = "cnh")
private Long cnh;
@Column(name = "cnpj")
private String cnpj;
@Column(name = "nome_fantasia")
private String nomeFantasia;

public Cliente() {
}

public Cliente(Long cpf) {
    this.cpf = cpf;
}

public Cliente(Long cpf, String nomeCompleto, String dataNascimento, String email, Long cnh) {
    this.cpf = cpf;
    this.nomeCompleto = nomeCompleto;
    this.dataNascimento = dataNascimento;
    this.email = email;
    this.cnh = cnh;
}

public Long getCpf() {
    return cpf;
}

public void setCpf(Long cpf) {
    this.cpf = cpf;
}

public String getNomeCompleto() {
    return nomeCompleto;
}

public void setNomeCompleto(String nomeCompleto) {
    this.nomeCompleto = nomeCompleto;
}

public String getDataNascimento() {
    return dataNascimento;
}

public void setDataNascimento(String dataNascimento) {
    this.dataNascimento = dataNascimento;
}

public String getEmail() {
    return email;
}

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

public Long getCnh() {
    return cnh;
}

public void setCnh(Long cnh) {
    this.cnh = cnh;
}

public String getCnpj() {
    return cnpj;
}

public void setCnpj(String cnpj) {
    this.cnpj = cnpj;
}

public String getNomeFantasia() {
    return nomeFantasia;
}

public void setNomeFantasia(String nomeFantasia) {
    this.nomeFantasia = nomeFantasia;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (cpf != null ? cpf.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Cliente)) {
        return false;
    }
    Cliente other = (Cliente) object;
    if ((this.cpf == null && other.cpf != null) || (this.cpf != null && !this.cpf.equals(other.cpf))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "alocar.java.models.Cliente[ cpf=" + cpf + " ]";
}

}

我需要它接受大于限制的值(我知道......但我也知道你知道我在说什么)]:)

标签: javalong-integer

解决方案


推荐阅读