0
What are get and set ?
Like getters and setters
5 Respostas
+ 6
it's for encapsulation purpose (data hiding) where sometimes you don't want the data of your object to be modified simply by anyone.
You use getter and setter when your variable In the objects are private
By using setter method, you can change the objects variable, by calling the method through the object
By using getter method, you can get the value of the variable in your object, through calling the method.
And the method is the only way to access or alter the variable of the object
example:
class Car{
private double petrolVolume;
public double getPetrolVolume(){
return petrolVolume;
}
public void setPetrolVolume(double volume){
petrolVolume = volume;
}
}
+ 6
get and set are mostly used in Properties and Indexers.
If you make a property like this :
public string Name { get; set; }
It will automatically make two functions for that property :
private Name;
public string get_Name() { return Name; }
public void set_Name(string value) { Name = value; }
Sorry don't know java. The code is in C#.
+ 5
Well this question is answered.
So who ever enters this question, please post on this thread :
https://www.sololearn.com/discuss/293240/?ref=app
+ 1
Thank u very much
+ 1
Thanks for the info.