Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

tutorial 4 - properties attribute

This is a discussion on tutorial 4 - properties attribute within the Object Oriented Programming - tutorials forums, part of the Flash English category; We carry on with the articles/lessons about Object Oriented Programming. We saw last time how to create a method ...


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

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-10-07, 06:46
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
tutorial 4 - properties attribute

We carry on with the articles/lessons about Object Oriented Programming.

We saw last time how to create a method for an Actionscript 3.0 class .

As Flash CS3 has also implemented some attributes for the class property, it is time for us to study the difference between a property with private and public attribute or protected and internal ones.

We will see the importance to assign and define the correct attribute to our properties.
To do so is an important point for the OOP!

Let us see a few examples?

private ? public ? protected and internal

The attributes available for an Actionscript 3.0 property are four:
- private: the property are accessible only from inside the class
- public: the property is accessible from inside the class, from instances of the class itself and directly if implemented of the static attribute (seen next)
- protected: the property is accessible only from inside the class or from one of its subclasses (seen further on when we will talk about Inheritance)
- internal: the property is accessible from inside the full package
The most commonly used are the private and static ones.

I create a simple class ?Quarta.as?, a Document Class associated to ?quarta.fla?.
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		public function Quarta()
		{
			
		}
	}
}
I define a property:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		private var nome:String='Filippo';
		
		public function Quarta()
		{
			trace(nome);
		}
	}
}
As seen in the second lesson, we obtain the following output:
Quote:
Filippo
The value of the property ?nome? has been succesfully retrieved, as the property is accessible from inside the class. In fact, I called it from inside it, precisely from its building function.

Let us assign the public attribute to Quarta:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		public var nome:String='Filippo';
		
		public function Quarta()
		{
			trace(nome);
		}
	}
}
We obtain the same output.

Let us assign the protected attribute:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		protected var nome:String='Filippo';
		
		public function Quarta()
		{
			trace(nome);
		}
	}
}
Once more, we obtain the same output.

Let us assign the internal attribute:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		internal var nome:String='Filippo';
		
		public function Quarta()
		{
			trace(nome);
		}
	}
}
Even with the internal attribute, the output stays the same.

Conclusion:
The property is always accessible from inside the class itself whatever attribute is used.

To show the difference between the attributes, we must be able to create an instance of the class.

I create a new class ?Miles.as?:
Code:
package
{
	public class Miles
	{
		public var nome:String='Davis';
		
		public function Miles()
		{
			trace('la classe Miles è stata istanziata');
		}
	}
}
This class does not extend the MovieClip Class, meaning it does not inherit its methods and properties from the MovieClip Class.
It is a simple class with a public property: none.
In the building function, a trace is placed and will be carried out once the class is instantiate.

Let us see how to instantiate it from the Document Class ?Quarta.as?:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		public function Quarta()
		{
			var miles:Miles=new Miles();
		}
	}	
}
- var miles: I create a local variable named ?miles?
- :Miles: I tell Flash it is of Miles type. Doing so, Flash will look for a class named Miles
- =new Miles(); I instantiate the class Miles and I obtain this output:
the class Miles has been instantiate

Important:
I could also declare ?miles? the following way:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		private var miles:Miles;
		
		public function Quarta()
		{
			miles=new Miles();
		}
	}	
}
in this case, I declare a property to the class ?Quarta.as? and I name it ?miles? of Miles type.
I then instantiate Miles from inside the building function. At that point, the property ?miles? is an instance of the class Miles.

Another way would be to declare ?miles? as follow:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		private var miles:Miles=new Miles();
		
		public function Quarta()
		{
			
		}
	}	
}
in this example, I declare the property ?miles? of Miles type and I instantiate it at the same time as the declaration.

Why have I done all those examples?
So all of you can understand properly what a class instance is.
The property ?miles? of ?Quarta.as? is an instance of the class Miles.

Let us now see the best part and go back to the attributes of a property.

What would happen if we wanted to retrieve the property ?nome? of the class Miles?

Let us call it from its own instance:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		private var miles:Miles=new Miles();
		
		public function Quarta()
		{
			trace(miles.nome);
		}
	}	
}
in the building function, I call ?Miles.nome? and I obtain the following output:
the class Miles has been instantiate
Davis
Meaning, that I can retrieve a property of Miles from one of its instance if the property has a static attribute (seen next).

If we assign the private attribute to the property ?nome? of ?Miles.as?, Flash returns the following error:
1178: Attempted access of inaccessible property nome through a reference with static type Miles.
Basically, Flash tells us that we are trying to access a property, which is not accessible, because:
- private: the property are accessible only from inside the class

If we assign the protected attribute, we obtain the same error because:
- protected: the property is accessible only from inside the class or from one of its subclasses
?Miles? does not extend ?Quarta? and so ?Miles? is not accessible from ?Quarta?.

If we assign the internal attribute, we obtain the following output:
the class Miles has been instantiate
Davis
As we can see, in this case, ?nome? in Miles is accessible from an instance of Miles because:
- internal: the property is accessible from inside the full package
As ?Quarta.as? and ?Miles.as? are in the same package, ?nome? in ?Miles? is accessible from an instance of ?Miles.as?

We also could do the following:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		private var miles:Miles=new Miles();
		
		public function Quarta()
		{
			trace(miles.nome);
		}
	}	
}

class Miles
{
	internal var nome:String='Davis';
	
	public function Miles()
	{
		trace('la classe Miles è stata istanziata');
	}
}
We define ?Miles? in the file ?Quarta.as? but outside the package brackets.
We omit the public attribute, which defines the class Miles (public class Miles) as Flash only accept that attribute if included into the package brackets.

Static attribute of a property

At the top of the four attributes seen until now, an Actionscript 3.0 property can have an attribute: static.

This attribute allows the property to be static, meaning that it can be called directly without the need to instantiate the class.

To understand better, we saw that from an instance of the class Miles (?miles?) we could access its property: nome.

If we had tried to call the property ?Miles.nome?, Flash would return an error that the property is not accessible as it does not have a static attribute.

Let us look at an example and go back to the two original classes written at the beginning of this lesson:

Class Quarta.as
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		public function Quarta()
		{
			
		}
	}
}
Class Miles.as
Code:
package
{
	public class Miles
	{
		public var nome:String='Davis';
		
		public function Miles()
		{
			trace('la classe Miles è stata istanziata');
		}
	}
}
Let us add the static attribute to the property ?nome? of ?Miles.as?, the following way:
Code:
package
{
	public class Miles
	{
		public static var nome:String='Davis';
		
		public function Miles()
		{
			trace('la classe Miles è stata istanziata');
		}
	}
}
we can access to ?nome? of Miles from ?Quarta.as? the following way:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		public function Quarta()
		{
			trace(Miles.nome);
		}
	}	
}
As you can see, I did not instantiate the class Miles.
I directly called ?nome? with ?Miles.nome? and obtained the following output:
Davis
In fact the trace in the building function of ?Miles? has not been carried out as the class Miles has not been instantiate.
Obviously, ?nome? of Miles has also a public attribute otherwise, it would not be accessible because:

- private: the property are accessible only from inside the class
- public: the property is accessible from inside the class, from instances of the class itself and directly if implemented of the static attribute

If we wanted to modify the value of ?nome:
Code:
package
{
	import flash.display.MovieClip;
	
	public class Quarta extends MovieClip
	{
		public function Quarta()
		{
			Miles.nome='Porgy';
			trace(Miles.nome);
		}
	}	
}
we would obtain the following output:
Quote:
Porgy
Source files:
Attached Files
File Type: zip tut_4.zip (15.1 KB, 42 views)

__________________

 


I recommend: Essential Actionscript 3.0

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

Last edited by Flep; 05-06-08 at 13:45..
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 4 - le funzioni Flep Actioscript 3.0 base - tutorials 4 05-01-09 17:45
Tutorial 7 - the packages Flep Object Oriented Programming - tutorials 3 13-11-07 02:42
Tutorial 5 - attribute of the methods Flep Object Oriented Programming - tutorials 0 15-10-07 06:43
tutorial 2 - the properties of my class Flep Object Oriented Programming - tutorials 0 27-09-07 06:36
Tutorial Cercasi joyz Actionscript 3.0 base 22 30-07-07 10:52


All times are GMT. The time now is 00:52.

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