I've had it explained a few ways, but for the life of me I can't wrap my head around it. Maybe someone here has a better way of explaining it.
I don't understand the point of accessors (get/set). Why bother?
I'm blockheaded.
Re: I'm blockheaded.
I can only guess what you're talking about - I'm not a programmer. However, there's Google.
- delinquent
- Match Winner
- Posts: 760
- Joined: Sat Jul 07, 2012 3:07 am
Re: I'm blockheaded.
I still don't really understand the point, I've seen those posts before. Take the second one, for example.
I create an int (number) and I call it "Age":
The example in the second post calls accessors a "layer of protection". Well, I can immediately set the value of Age to 0(zero) anyway:
Instead of doing the following:
Or something to that effect. But... why bother?
Note that in my pseudo example Age and age are two separate variables.
I create an int (number) and I call it "Age":
Code: Select all
public int Age;
Code: Select all
public int Age = 0;
Code: Select all
private int age;
public int Age()
{
if (age == null)
{
age = 1
MessageBox.Show("1 added to age because it didn't get assigned a value")
}
else
{
continue;
}
get{return age};
set{age = value};
}
Note that in my pseudo example Age and age are two separate variables.
Re: I'm blockheaded.
it is a process that ensure additional safety. Very useful on big programs.
Re: I'm blockheaded.
Just a text interpretation: The range checking part of the second text (the condition statement thing) sounds to me like it's meant to drastically reduce the time/attempts needed to find a correct entry and sort out/skip the incorrect ones that would otherwise affect your program in some way - like a warehouse detective who has a secret list of the only 10 shoplifters in his town.
- kyle
- Reverse Outside Corner Grinder
- Posts: 1876
- Joined: Thu Jun 08, 2006 3:33 pm
- Location: Indiana, USA, Earth, Milky Way Galaxy, Universe, Multiverse
- Contact:
Re: I'm blockheaded.
You may be able to do that in the case of primitive types, but lists say you want to make in and array of variable length. You don't know the length until it is initialized.delinquent wrote: The example in the second post calls accessors a "layer of protection". Well, I can immediately set the value of Age to 0(zero) anyway:
Code: Select all
public int Age = 0;
again with this second one you are limiting the value of the age variable. If you make it public any other code that uses it can manipulate it, potentially making it a negative value.
If age were public
Code: Select all
int main()
{
Person joe;
joe.age = -1; //therefore bypassing the protection in the Person class to Greentree that age is positive.
return 0;
}
Also if you are using objects within the object, you may want to return a deep copy instead of a pointer to the object.
Re: I'm blockheaded.
Dont feel bad, OOP has never made much sense to me. I do itertative or functional.