Last time in
lesson 5 , we saw how to use the methodís attributes of an Actionscript 3.0 class.
We also saw how to 'nest' a property using a method that once called, returns the value of that property.
Another way to 'nest' a property with Actionscript 3.0 is using the so-called methods getter&setter.
I already talked about them in the article: getter & setter and now we will see this argument in depth.
I create a FLA and save it as 'sixth.fla'.
I associate to it the following Document Class (Sixth.as):
Code:
package
{
import flash.display.MovieClip;
public class Sixth extends MovieClip
{
public function Sixth()
{
}
}
}
Next, I create another class (Miles.as):
Code:
package
{
public class Miles
{
public function Miles()
{
}
}
}
to which I assign a property of type Number named _test of a value equal to 100:
Code:
package
{
public class Miles
{
private var _test:Number=100;
public function Miles()
{
trace(' Miles class has been instantiated correctly ');
}
}
}
to which I assign a property of type Number named _test of a value equal to 100:
I use the underscore before the name as a good rule to follow for a property nested with the method getter&setter (seen next).
I instantiate the Miles class from Sixth:
Code:
package
{
import flash.display.MovieClip;
public class Sixth extends MovieClip
{
private var miles:Miles;
public function Sixth()
{
miles=new Miles();
}
}
}
I obtain obviously this output: Miles class has been instantiated correctly.
Nothing new up till now.
We saw in lesson 5 how to create a method that returns the value of the variable _test
without the need to render that variable public.
With the method getter&setter, I can do it the following way:
Code:
package
{
public class Miles
{
private var _test:Number=100;
public function Miles()
{
trace(' Miles class has been instantiated correctly ');
}
public function get test():Number
{
return _test;
}
}
}
public function: I define the method as public
get: so that Flash can access the value of the property _test
test: the name of the method (it could be named anyway you wish but it is of good use to assign to the get the same name as the nested property without the underscore)
:Number as _test has a data type Number, the method will return a number
I call test from Sixth.as:
Code:
package
{
import flash.display.MovieClip;
public class Sixth extends MovieClip
{
private var miles:Miles;
public function Sixth()
{
miles=new Miles();
trace(miles.test);
}
}
}
TO NOTICE: miles.test
I did not call miles.test() such as a normal call to a method but simply using miles.test. It seems that Miles.as has a property named test but it is not true. The get does all the job on its own.
The property _test of Miles is now nested and read-only. In fact, if we try to change its value:
Code:
package
{
import flash.display.MovieClip;
public class Sixth extends MovieClip
{
private var miles:Miles;
public function Sixth()
{
miles=new Miles();
trace(miles.test);
miles.test=200;
}
}
}
Flash would return the following error:
1059: Property is read-only.
If we wanted to allow the access to the property _test in runtime, we will need to add a setter the following way:
Code:
package
{
public class Miles
{
private var _test:Number=100;
public function Miles()
{
trace('la classe Miles Ë stata istanziata correttamente');
}
public function get test():Number
{
return _test;
}
public function set test(n:Number):void
{
_test=n;
}
}
}
: I define the method as public
set so that Flash can change the value of the property
test the name of the method (the same name as the one for the ësetter)
(n:Number) the method requires a parameter of type Number (the same type as the nested property)
:void the method does not return a value
We can change the value of _test the following way:
Code:
package
{
import flash.display.MovieClip;
public class Sixth extends MovieClip
{
private var miles:Miles;
public function Sixth()
{
miles=new Miles();
trace(miles.test);
miles.test=200;
trace(miles.test);
}
}
}
miles.test=200;
once again (such as in the setter), the method set.test is called as if it was a property and we obtain the following output:
Miles class has been instantiated correctly
100
200
Source files: