0

Why does the get and set keywords exist? They seem to be useless for me...

For example:

public function set player_X(x:Number):void
{
    player.x = x;
}

public function setPlayerX(x:Number):void
{
    player.x = x;
}

These two functions does the same thing right? And the second one does not use the set keyword.

Erik W
  • 2,590
  • 4
  • 20
  • 33
  • May be: http://stackoverflow.com/a/2894249/1206613 – Ivan Chernykh Jan 10 '15 at 16:11
  • 2
    The first function you can use like this: `object.player_X = n;` the second function you have to use like this: `object.player_x(n);`. It's simple convenience. –  Jan 11 '15 at 00:23

1 Answers1

1

The difference is that the set method is implicitly called when you set a property of the same name.

You do not have to type the ( ) that do the function call but assign the value via =.

player_X = 5;

vs.

setPlayerX(5);

It can help with information hiding as to the user of a class, this is appears to be a property and can be used as such.

null
  • 5,207
  • 1
  • 19
  • 35