Erik W
Erik W

Reputation: 2628

AS3 - Get and Set

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.

Upvotes: 0

Views: 314

Answers (1)

null
null

Reputation: 5255

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.

Upvotes: 1

Related Questions