4
I created the advisory methods Setter and getter for the balance attribute.
It is correct to use these advisors within each other? One being used as parameter for the other.
More specifically the getSaldo()
being a parameter of setSaldo()
that is within the method sacar()
.
The line:
this.setSaldo(this.getSaldo() - saque);
Example code in Java:
public class ContaBanco {
private int saldo;
public int getSaldo() {
return saldo;
}
public void setSaldo(int saldo) {
this.saldo = saldo;
}
public void sacar(int saque){
//this.saldo = this.saldo - saque;
this.setSaldo(this.getSaldo() - saque);
}
}
The code itself worked. But I don’t know if it’s correct and within the standards that way.
The subject of the test:
public static void main(String[] args) {
ContaBanco c1 = new ContaBanco();
c1.setSaldo(500);
c1.sacar(100);
}
Very grateful for the clarifications @Piovezan and @Maniero From what I understand, in this example the use of advisors would be optional to facilitate the writing and understanding of the code, being more necessary only in cases of code with more "resources/complex" for better use of setters and getters, without writing code unnecessarily
– rafax23
Now you can vote on any questions and answers from the site you find interesting.
– Maniero
@rafax23 If you’re referring to the private accessors (self-encapsulation) that Fowler spoke about, yes (by the way that would be the same term? Advisor? Advisor? I was in doubt now, I think there is no harass in PT, only advise, I call the getters + setters jointly of manipulators). If you’re talking about public or protected accessors, then I prefer to say that they should only exist when they make sense for the user interface you’re willing to offer to customers of the objects in that class, without necessarily having a ratio of 1 to 1 between the same and the internal fields.
– Piovezan