- Home /
Advantages of properties?
I feel myself being confused by Properties. Say for instance I am making a Galaga remake or something and I have a class ShipClass which a baseSpeed variable that I want all of the enemies and player to inherit from. From there they can modify that variable which means I can control how fast different enemies go or I can give the player a power up which modifies speed for a little bit. I did this through properties with my ShipClass looking something like this.
using UnityEngine; using System.Collections;
public class ShipClass : MonoBehaviour { //private int health = 100; public int speed = 10;
public int propSpeed
{
get
{
return speed;
}
set
{
speed = value;
}
}
}
And then my Player control would look something like this:
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour
{
ShipClass player = new ShipClass ();
void Start ()
{
//useless for now
}
void Update ()
{
if(Input.GetKey("a"))
transform.Translate (-1 * player.propSpeed * Time.deltaTime, 0, 0);
if (Input.GetKey ("d"))
transform.Translate (1 * player.propSpeed * Time.deltaTime, 0, 0);
}
}
So my question really is there any advantage in using a property rather than just making a getter and setter method as needed? Or is this purely a programer's preference in which way you implement this? Like choosing between properties, methods, or just making the variable public and let the gods decide the fate of your program.
I hope this is clear enough to understand. Thanks in advance
Answer by oatsbarley · Jan 24, 2014 at 04:32 PM
See: What is the difference between a field and a property in C#? - Stack Overflow
Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.
Kent points out that Properties are not required to encapsulate fields, they could do a calculation on other fields, or serve other purposes.
GSS points out that you can also do other logic, such as validation, when a property is accessed, another useful feature.
The two biggest benefits, as mentioned in the quote, are being able to change the field without it affecting users of the class, and being able to perform calculations on get and set. It's handy being able to modify the internals of a class without having to then go to every place the class is referenced and make modifications.
Answer by Owen-Reynolds · Jan 24, 2014 at 07:08 PM
There is no advantage is having a regular variable, such as speed, start life as a property (which is the same thing as saying don't bother writing get/set methods if they don't do anything.)
The advantage to properties is you can easily later convert if you need to. Which, as noted above, is like magically pasting code in front of every speed=X
everywhere.
In your example, don't use propSpeed. Just have everyone use player.speed=...
to access the variable. Next, imagine you want to enforce a max speed, and maybe also make a rocket emitter change based on speed. Now you change speed into a property: make a new private class variable named _speed
, and a getter/setter named speed. Have the setter check the max and change the emitter.
Everywhere in the program where you were using the old variable, speed=
, is now a magically doing that new stuff, as a property.
In the old days, w/o properties, you'd have to write a method named setSpeed(float newSpeed);
, which would have the same code (check the max, change the emitter.) But then you'd need to go through every other file, changing speed=X
to setSpeed(X);
. It wouldn't be that bad, since you'd set speed to private and follow the errors (which would force you write also write getSpeed().)