hi,
i wanna ask about setter and getters what are they used for and an example of setter and getter
and thanks,hi, can u give me information about setter and getters like in C#
1_ why we use thim
2_ example of this method and explan
and thanks,
hi,
i wanna ask about setter and getters what are they used for and an example of setter and getter
and thanks,hi, can u give me information about setter and getters like in C#
1_ why we use thim
2_ example of this method and explan
and thanks,
They are public functions that can be called to GET or SET the value of a private variable.
public myclass {
private int _score;
public int MyScore {
get { return _score; }
set { _score = value; }
}
}
So in the above example you can access MyScore instead of _score. Totally useless and a complete waste of time and speed in my opinion, but object oriented purists love them.
I’m probably not going to say too much that hasn’t already been said, but I’m going to add a few things. Properties are function calls for getting and setting a value. They are most useful for abstraction. Properties are an easy way of hiding implementation from other classes. @Aldonaletto gave a good example of when you would do this. Another good one is getting the length of a vector.
public float magnitude {
get {
return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
}
This example provides 2 good reasons why to use properties:
If you use properties, its important to be consistent. Properties and variables are similar, but there not the same. Someone trying to figure out your code later will probably thank you for being consistent, especially if they are reflecting over it. A useful feature when the property would be a simple getter/setter:
public float Value {
get; set;
}
Another useful feature of properties is that you can put them in interfaces and abstract classes. This is an advantage they have to variables. My favorite example:
public abstract class Animal {
public abstract int Limbs { get; }
}
public class Dog : Animal {
public override int Limbs {
get {return 4; }
}
}
This way you can get the number of limbs on every animal and know that it is a safe operation.
One of the most interesting OOP ideas is the concept of property. A property looks like an ordinary variable, but the big difference is the use of getter and setter, routines that are internally called when you read or assign to a property. Most GameObject “variables” are actually properties, each one with its own getter and setter. In most cases, there exists an ordinary private variable that stores the property value, but the getter and setter do a lot of extra things when you read or assign to the property.
The parent variable, for instance, is a property. Probably the getter is a simple routine like in the @ptdnet example, that only returns the value of the private _parent (or whatever they named it) variable. The setter, on the other hand, is way more complicated: when you assign a transform to the parent variable, the setter must calculate and assign localPosition, localRotation and localScale, not to mention several other internal changes related to physics or to the game object structure. If it wasn’t a property, you should do all this extra work by hand, instead of just assigning the value to parent (thanks God for properties!)
Most times, the getter is simpler than the setter, but not always: eulerAngles, for instance, is a completely virtual property; there’s no such a thing like a private variable Vector3 _eulerAngles - the getter converts the rotation internal value to Euler angles, and returns a Vector3. The declaration of the property eulerAngles probably is something like this:
public Vector3 eulerAngles { get { return ConvertRotationToEuler(_rotation); } set { _rotation = ConvertEulerToRotation(value); } }
Where the functions ConvertRotationToEuler and ConvertEulerToRotation are very complex routines that run behind the scenes.
If you just need to assign to a variable or read its contents, don’t mind - like @ptdnet said, using setter/getter in this case is a complete waste of time. But if you need to do other things anytime this variable is modified or read, then a property is the answer.