Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Tutorial 5 - attribute of the methods

This is a discussion on Tutorial 5 - attribute of the methods within the Object Oriented Programming - tutorials forums, part of the Tutorials category; We saw in lesson 4 of OOP with Flash CS3 , the different type of attributes that we can combine with ...


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
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 15-10-07, 06:43
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Tutorial 5 - attribute of the methods

We saw in lesson 4 of OOP with Flash CS3, the different type of attributes that we can combine with a property and how to declare them.
The attributes associated to a method are equal of the ones for a property and they have the same characteristics.

An important point is as example:
If we have a class with a property named "nome", we can assign it a private attribute so that no one using our class can access its value and our class can carry out a fixed task without having the property"s functions modified (otherwise the result would be altered).

The problem using the private attribute is that its value would not be available even if we wanted to only retrieve its value and reuse it during the course of developing our application.

To make it accessible without changing its private attribute, we can create a public method (for example: "prendiNome") which once called will retrieve the value of the property "nome".

How"
As the method is placed inside the class (it is one of the class method), it has access to the property "nome".

Let us look at concrete examples"

Read more

I create a FLA and save it as "quinta.fla".
I create the Document Class saved as "Quinta.as":
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quinta extends MovieClip
	{
		private var elenco:Elenco;
		
		public function Quinta()
		{
			
		}
	}
}
The Document Class has a property of type "Elenco", meaning a variable of type "Elenco" or also an instance of the class Elenco (seen next)

Class Elenco.as
Code:
package
{
	public class Elenco
	{
		private var nome:String='Miles';
		
		public function Elenco()
		{
			
		}
	}
}
has a private property named "nome" and contains a string value.

Next, we instantiate the class Elenco:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quinta extends MovieClip
	{
		private var elenco:Elenco;
		
		public function Quinta()
		{
			elenco=new Elenco();
		}
	}
}
If we try to retrieve the value of "nome" from Elenco the following way:
trace(elenco.nome);
Flash would rightly return this error:
1178: Attempted access of inaccessible property nome through a reference with static type Elenco.
This has already been seen and explained in lesson 4.

However, if we really needed to retrieve that value, would it mean that the property "nome" shall be public"
The answer is no. We could implement a method of the class elenco the following way:
Code:
package
{
	public class Elenco
	{
		private var nome:String='Miles';
		
		public function Elenco()
		{
			
		}
		
		public function prendiNome():String
		{
			return nome;
		}
	}
}
calling the method "prendiNome", we can retrieve the value of "nome". The method is public and the property is private so the method can retrieve its value:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quinta extends MovieClip
	{
		private var elenco:Elenco;
		
		public function Quinta()
		{
			elenco=new Elenco();
			trace(elenco.prendiNome());
		}
	}
}
we now have the following output:
Miles

In this case, "nome" of elenco is not accessible and so not modifiable but it is readable via the public method "prendiNome".

Basically, we have encapsulated the property "nome" of Elenco.

In the next lesson, we will see another way to do it using the methods getter&setter.

Static methods

Like the properties, the method can also have a static attribute. Its use and function are the same as with the property.

Let us look at an example:
Code:
package
{
	public class Elenco
	{
		private var nome:String='Miles';
		
		public function Elenco()
		{
			
		}
		
		public function prendiNome():String
		{
			return nome;
		}
		
		public static function provaStatico():void
		{
			trace('il metodo statico ProvaStatico è stato chiamato correttamente');
		}
	}
}
I added a static and public method to the class Elenco.

I call the method from the Document Class:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quinta extends MovieClip
	{
		private var elenco:Elenco;
		
		public function Quinta()
		{
			elenco=new Elenco();
			trace(elenco.prendiNome());
			Elenco.provaStatico();
		}
	}
}
Obviously, I do not call it from an instance of "elenco" but directly (as seen for the properties):
Elenco.provaStatico();
and I obtain this output:
Miles
the static method provaStatico has been correctly called

Accessibility of a non-static property from a static method and vice versa

Let us now try to access to "nome" of Elenco from the static method "provaStatico":
Code:
package
{
	public class Elenco
	{
		private var nome:String='Miles';
		
		public function Elenco()
		{
			
		}
		
		public function prendiNome():String
		{
			return nome;
		}
		
		public static function provaStatico():void
		{
			trace('il metodo statico ProvaStatico è stato chiamato correttamente');
			trace(nome);
		}
	}
}
Keeping always the call from the Document Class using "Elenco.provaStatico();", Flash returns the following error:
1120: Access of undefined property nome.

It means that the static methods cannot access to a static property.
Flash even tells us that the property "nome" is undefined.

Let us now try the opposite and try to access a static property from a non-static method.

The class Elenco would be as followed:
Code:
package
{
	public class Elenco
	{
		private static var nome:String='Miles';
		
		public function Elenco()
		{
			
		}
		
		public function provaNonStatico():void
		{
			trace('il metodo statico ProvaStatico è stato chiamato correttamente');
			trace(nome);
		}
	}
}
The property "nome" is now static and the method "provaNonStatico" is non-static.

The call made from the Document Class is as followed:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quinta extends MovieClip
	{
		private var elenco:Elenco;
		
		public function Quinta()
		{
			elenco=new Elenco();
			elenco.provaNonStatico();
		}
	}
}
elenco.provaNonStatico(); is a call of the event "provaNonStatico" from the instance of the class Elenco (elenco) placed in the Document Class (also seen in lesson 4 about the properties).
I obtain the following output:
the static method Provastatico has been correctly called
Miles

In brief:
- a static method can not access to a non static property
- a non static method can access to a static property
Source files:
Attached Files
File Type: zip tutorial_5.zip (6.7 KB, 36 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:47..
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 - the methods Flep Object Oriented Programming - tutorials 6 27-07-08 02:21
Encapsulation, Getter and Setter methods Flep Tutorials 6 08-02-08 12:34
Tutorial 3 - the loops Flep Actionscript for beginners - tutorials 0 21-11-07 06:50
tutorial 4 - properties attribute Flep Object Oriented Programming - tutorials 0 05-10-07 06:46
getPixel/setPixel methods of the BitmapData Class Flep Tutorials 0 29-09-07 10:04


All times are GMT. The time now is 04:35.


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