0
What is the basic difference of set
and get
in object-oriented programming?
0
What is the basic difference of set
and get
in object-oriented programming?
5
Assuming you want to know about a property or prefix method set
and get
, this pattern is used to abstract a state that must be internal to the object. They are used as facilitators in cases of fields that must be accessed in a simple and direct way for take the value of an attribute (get
) or to change the value of this attribute (set
).
They are also called accessors and changers.
In general it is said that they cause a encapsulation, because real encapsulating is create methods that do something special that does not encapsulate only the access or assignment, is to create a meaning for the method. It is an abstraction but not an encapsulation.
They are used to hide the detail of how the state is stored and/or calculated, validated, etc. Hiding the detail is an abstraction.
There is a chain that says they should always be used, even if they do not do any further processing other than take the value (get
) or assign the value (ser
) to the class variable. So if one day you need to put something just change the implementation of this pair of methods and everyone who consumes the object starts to use the new way. This is a indirect.
But there are those who are more pragmatic and only use when necessary, because in general it is easy to refactor.
There are still those who say that they are always a mistake because the methods should have a function of their own, just let take or change the data of a field is to expose detail in disguise.
Some languages have facilities for this pair of methods to look like a simple variable, is the case of C#. It can be seen as it is different in Java.
They can be used separately. In fact it is common to have one get
and not a set
. In some cases the set
is private or protected so that only the object itself can access it.
Behold arguments to use in Java.
There’s a explanation when use in PHP.
Behold as used in Python.
Example in pseudocode:
class Cliente { //exemplo bem meia boa
private string nome; //note que é privado
public string getNome() => return nome; //aqui pega a variável interna e retorna
public void setNome(string nome) => this.nome = nome; //aqui joga o parâmetro na variável da classe
private decimal saldo;
public decimal getSaldo() => return saldo - 100; //dá uma margem de erro
public void setSaldo(decimal valor) => if (valor > 200) saldo = valor; //só aceita um mínimo de 200
}
3
The method (GET)
serves to recover a data and the method (SET)
is used to modify a data
In fact, there are many good reasons to consider using methods (getters and setters) instead of directly exposing fields of a class - beyond the encapsulation argument and facilitating future changes.
Here are some of the reasons:
This is what I’ve been looking for ?
That’s right. Thank you
Browser other questions tagged oop getters-setters
You are not signed in. Login or sign up in order to post.
Can you elaborate more on what you want to know? Is any of this not what you want? https://answall.com/questions/tagged/getters-setters?sort=votes&pageSize=50
– Maniero