Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Tutorial 6 - Getter & Setter

This is a discussion on Tutorial 6 - Getter & Setter within the Object Oriented Programming - tutorials forums, part of the Tutorials category; Last time in lesson 5 , we saw how to use the methodís attributes of an Actionscript 3.0 class. ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Tutorials > Object Oriented Programming - tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  6 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 23-10-07, 07:00
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Tutorial 6 - Getter & Setter

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
File Type: zip tut_6.zip (5.9 KB, 38 views)

__________________

 


I recommend: Essential Actionscript 3.0

- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
- I do not reply technicians pvt messages. Open a thread !

Last edited by Flep; 05-06-08 at 13:48..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Tutorial 3 - i cicli Flep Actioscript 3.0 base - tutorials 12 23-06-08 23:58
Object Oriented Programming - lezione 6 - Getter & Setter Flep Programmazione Orientata agli Oggetti - tutorials 1 14-06-08 18:36
Tutorial 4 - the functions Flep Actionscript for beginners - tutorials 1 21-05-08 10:31
Encapsulation, Getter and Setter methods Flep Tutorials 6 08-02-08 12:34
Incapsulamento e metodi Getter & Setter Flep Articoli e tutorials 15 14-11-07 13:58


All times are GMT. The time now is 05:25.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406