如何在这种情况下正确使用Getter

时间:2019-04-15 00:44:51

标签: java intellij-idea getter-setter

我整天都在寻找解决方案,但是首先,我想对Java和面向对象说一说是全新的,所以请对我轻松点

然后,这是我到目前为止编写的一些代码

主类

package br.edu.fatecmm.study;

public class Main {

    public static void main(String[] args) {

        Pessoa ppl = new Pessoa();
        Conta gamer = new Conta();

        gamer.setLogin("kouhei");
        gamer.setPassword("mypass");

        ppl.setNome("Qualquer");
        ppl.setSobrenome("Nome");
        ppl.setJogador(gamer);

        ppl.imprimePessoa();
        gamer.imprimeConta();

    }
}

佩索阿课程

package br.edu.fatecmm.study;

public class Pessoa {
    private String nome, sobrenome;
    private Conta jogador;

    public void imprimePessoa(){
        System.out.println("nome = " + nome);
        System.out.println("sobrenome = " + sobrenome);
        System.out.println("jogador = " + jogador.toString());
        System.out.println(" ");
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSobrenome() {
        return sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public Conta getJogador() {
        return jogador;
    }

    public void setJogador(Conta jogador) {
        this.jogador = jogador;
    }
}

Conta类

package br.edu.fatecmm.study;

public class Conta {
    private String login, password;

    public void imprimeConta(){
        System.out.println("login = " + login);
        System.out.println("password = " + password);
        System.out.println(" ");
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

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

所以,当我运行上述代码时,会发生以下情况:

"C:\Program Files\Java\jdk-11.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.4\lib\idea_rt.jar=49901:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.4\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Kouhei\IdeaProjects\Study\out\production\Study br.edu.fatecmm.study.Main
nome = Qualquer
sobrenome = Nome
jogador = br.edu.fatecmm.study.Conta@3498ed
 
login = kouhei
password = mypass
 

Process finished with exit code 0

基本上,我想做的是,当我打电话时 ppl.imprimePessoa();

它应该返回nome + sobrenome,分别为“ Qualquer”和“ Nome”,然后返回jogador,应该为“ kouhei”和“ mypass”,但是,而不是kouhei和mypass返回,而是得到“ br .edu.fatecmm.study.Conta @ 3498ed“

我相信3498ed的记忆力正在下降,但我不确定,也许我说的话完全是愚蠢的,但是我什至不知道该寻找什么来研究这个问题

我尝试阅读有关Getter和Setters的内容,但没有帮助

我还希望不仅能在调用时检索“ kouhei / mypass” ppl.imprimePessoa();

但是我也想只检索“ kouhei”

我尝试过

ppl.setJogador(gamer.getLogin());

但它不起作用

几个小时以来,我一直在尝试许多不同的方法,并且看到了一些有关实例的信息,但是无法使其工作

预先感谢

1 个答案:

答案 0 :(得分:0)

调用默认的toString()时,返回的是对象的字符串表示形式。

如Java文档中所述:https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

如果要使用toString,则应在类Conta中覆盖它,如下所示:

@Override
public String toString(){
    return "login = " + login + "password = " + password;
}

如果您不愿意覆盖默认方法(尽管这样做没问题),另一种可能性是创建一个新方法,该方法返回类Conta中的String。喜欢:

public String jogadorString(){
    return "login = " + login + "password = " + password;
}

并将其在类Pessoa中用作jogador.jogadorString()而不是jogador.toString()