Citing Actionscript 3.0 hence the Object Oriented Programming, I can"t and don"t want to bypass the encapsulation and the Getter and Setter methods.
The encapsulation is a technique to protect/hide the properties of our Class.
This is because we could find ourselves in the situation of having to create a Class to then pass it to a customer or a developer, but not necessarily giving direct access to the properties of it.
For those new to programming, this could sound complex, but let"s play this feeling down. I"ll explain better, we"ve seen how to define properties in our classes:
Code:
package
{
import flash.display.MovieClip;
public class Filippo extends MovieClip
{
private var mano:MovieClip;
private var misura:Number;
public function Filippo()
{
}
}
}
In the Filippo class, mani and misura are 2 properties. So far we only have access to these properties the common way, i.e. directly:
Code:
package
{
import flash.display.MovieClip;
import flash.display.Shape;
public class Filippo extends MovieClip
{
private var mano:Shape;
private var misura:Number=100;
public function Filippo()
{
misura=400;
mano=new Shape();
mano.graphics.beginFill(0xFFCC00);
mano.graphics.drawRect(0,0,misura,misura);
mano.graphics.endFill();
addChild(mano);
}
}
}
If we initialized the class from the Timeline this way:
Var filo:Filippo=new Filippo();
To give access to the mano property, we"d have to change its definition from private to public, which isn"t good going.
If we wanted give access to the properties, but indirectly ( as they are private ) we have to define methods that return those properties. Here we have 2 ways:
Code:
package
{
import flash.display.MovieClip;
import flash.display.Shape;
public class Filippo extends MovieClip
{
private var mano:Shape;
private var misura:Number=100;
public function Filippo()
{
mano=new Shape();
mano.graphics.beginFill(0xFFCC00);
mano.graphics.drawRect(0,0,misura,misura);
mano.graphics.endFill();
addChild(mano);
}
public function dammiLaMisura():Number
{
return misura;
}
public function tieniLaMisura(n:Number):void
{
misura=n;
}
}
}
in this case we"d access the misura property from the Timeline this way:
var filippo:Filippo=new Filippo();
filippo.tieniLaMisura(400);
trace(filippo.dammiLaMisura());
or the Setter and Getter:
Code:
package
{
import flash.display.MovieClip;
import flash.display.Shape;
public class Filippo extends MovieClip
{
private var mano:Shape;
private var _misura:Number=100;
public function Filippo()
{
mano=new Shape();
mano.graphics.beginFill(0xFFCC00);
mano.graphics.drawRect(0,0,misura,misura);
mano.graphics.endFill();
addChild(mano);
}
public function set misura(n:Number):void
{
_misura=n;
}
public function get misura():Number
{
return _misura;
}
}
}
in this case we"d access the misura property this way:
var filippo:Filippo=new Filippo();
filippo.misura=400;
trace(filippo.misura);
In both cases we"ve implemented public methods to assign or receive a value from the private properties.
Some programmers ( myself included :P ) consider the first example a little uncomfortable, as it"s simpler:
filo.misura=400; (getter&setter)
instead of
filo.tieniLaMisura(400);
Stay tuned !