Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Results 1 to 1 of 1

Thread: Tutorial 6 - Getter & Setter

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Tutorial 6 - Getter & Setter

    flash templates
    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;
    		}
    	}
    }
    Code:
    public function
    : 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:
    Attached Files

+ Reply to Thread

Similar Threads

  1. Get Setter, e return
    By Kerotan in forum Actionscript 3.0 base
    Replies: 2
    Last Post: 21-04-10, 11:15
  2. Object Oriented Programming - lezione 6 - Getter & Setter
    By Flep in forum Programmazione Orientata agli Oggetti - tutorials
    Replies: 1
    Last Post: 14-06-08, 17:36
  3. Encapsulation, Getter and Setter methods
    By Flep in forum Tutorials
    Replies: 6
    Last Post: 08-02-08, 11:34
  4. Incapsulamento e metodi Getter & Setter
    By Flep in forum Articoli e tutorials
    Replies: 15
    Last Post: 14-11-07, 12:58
  5. Tutorial 8 the constants
    By Flep in forum Object Oriented Programming - tutorials
    Replies: 0
    Last Post: 30-10-07, 05:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts