3
In class Pessoa
declared a array of the kind Telefone
which stores 3 phone contacts in the set referred to below it for each contact assigns a type and a number.
I have a problem where I want to assign a new type to a person and número
using the setContactos()
referred to in the test class below.
public class Pessoa{
private Telefone[] contactos;
//....
public void setContactos(Telefone[] cont) {
for (int i = 0; i < contactos.length; i++) {
contactos[i].setNumero(cont[i].getNumero());
contactos[i].setTipo(cont[i].getTipo());
}
}
//....
}
public class Teste {
public static void main(String[] args) {
Telefone t1 = new Telefone("Casa", 123456);
Telefone t2 = new Telefone("telemovel", 123456);
Pessoa p1 = new Pessoa("Marta Pereira");
p1.setNIF(3454543);
p1.setContactos(t1); //erro incompatible types: Telefone cannot be converted to Telefone[];
System.out.println(p1.toString());
}
}
Getters and setters are a separate modification false insurance policy, they make sense when: 1-In a Setter, before updating the state in this object according to some input, we validate the input. 2 - The return type of a getter is an interface. So we dissociated the implementation from the exposed interface, I think it’s actually something taught out there in many Java/Android courses to make information "private", without explaining the concepts of building an object and why to do it
– ScrapBench
Reading clean code, there is a reference to this in chapter 6
– ScrapBench
Your method worked well. Thank you
– Elio Borges
Thanks for the @Scrapbench tip
– Elio Borges