OOP (Object Oriented Programming) is a programming concept (or technique), which treats data and functions as objects.
Important to this concept is to understand the difference between a Class and an Object.
- A class is a "blueprint" for an object, is a code template used to generate objects. It contins the instructions that define the properties and methods that an object can use.
- Objects are elements from the script that are defined to perform the instructions written in a class, and can use the properties and methods defined in the class.
- For example, to understand, you can think of a class as a blueprint for constructing an house. Many houses can be built from the same blueprint, so, the object is the house created according to that blueprint. You can build multiple objects that have the properties defined in the class.
The difference between Class and Object is important. If a class can be asimilated to a type of data, an object can be equated with a variable or with a value having a certain type of data. Practically, a class is a "factory" of objects, that produces objects with the same structure, having the same properties and methods.
ActionScript 3 contains several predefined classes, such as "Date class" used when working with date and time, "String class" for strings, and others; but you can also create your own classes.
To create a class use the class keyword, followed by it's name and it's body (contained between curly brackets). The properties and methods are defined in the class body.
- Properties are variables defined within the class.
- Methods are functions created within the class.
The general structure of a class is:
class ClassName { var property1; var property2; ................. function method1() { // Funtion code } function method2() { // Function Code } ................. }
package packageName { attribute class ClassName { attribute var property1; attribute var property2; ................. attribute function method1() { // Function code } attribute function method2() { // Function code } ................. } }- "packageName" is optional, for beginners it's better to not specify it, because needs extra things (explained below).
// create the package package { // define the class, with public attribute, a property "prop" and a method "metod" public class TestClas { public var prop:Number = 7; // Public property protected const HIDEN:int = 2; // "Protected" constant // Creating the method, takes an argument (Number type) public function metod(val:Number):void { // changes the value of the "prop" property, with the "val" parameter and the "HIDEN" constant this.prop = val + HIDEN; } } }- "this.prop" indicates the property "prop" from this class (the term this is added to indicate that it refers to the property of the current class, avoiding errors that can appear if in that function you use other parameters and variables with the same name).
import packageName.ClassName;OR:
import packageName.*
Once a class is created and saved in a file "ClassName.as", you must create an object instance of that class to can use its properties and methods in a script. This instance must be declared in the script written in the FLA document as any other object used in ActionScript 3 (such as Array, Date, etc., these are also classes), with the sintax:
var name_ins:ClassName = new ClassName();OR:
var name_ins:packageName.ClassName = new packageName.ClassName();(if the class is in a package with a name)
// Creates the object instance of the TestClas class var tst:TestClas = new TestClas(); // Checks the value of the "prop" property trace(tst.prop); // 7 // access the "metod()" method, which change the value of "prop" tst.metod(18); // checks "prop" trace(tst.prop); // 20- Because the FLA file and the TestClas class are in the same folder, Flash automatically imports this Class when an instance is created. When you access the properties and methods (through the instance), the instructions asociated to them in that class are executed in the script.
The constructor is a special function inside the class. It must have the same name as the class. This method is always "public" even if this attribute is not mentioned.
The constructor is automatically called when an instance of the respective class is created.
Another difference from other functions is that the Constructor not use the "return" instruction.
// create the package package { // define the class, with public attribute public class TestClas { public var prop:Number = 7; // Public property protected const HIDEN:int = 2; // "protected" constant // Creating the constructor public function TestClas(nr1:Number, nr2:Number) { // stores in a variable the arithmetic average of the "nr1" and "nr2" parameters var average_a = (nr1+nr2)/2; trace(average_a); // Returns in output the "average_a" value } // Create method (takes a Number type argument) public function metod(val:Number):void { // change the value of the "prop" property, a number given by the "val" parameter and the "HIDEN" constant this.prop = val + HIDEN; } } }- Notice that the contructor method has the same name as the class. The constructor is defined with two parameters ("nr1" and "nr2"), when an instance of this class is created, two values must be added as arguments (new TextClass(val1, val2)).
var tst:TestClas = new TestClas(7, 8);- In the Output panel will be displayed the number 7.5, fact that demonstrates that the Constructor method is called automatically and its code is executed when an object instance is created.
The variables (or properties) created in a class can be defined with a value or they can simply be declared without value. Then you can create a function to change or assign their value. This function is generically called Accessor method, it's the same as any other method, only that it's purpose is to attribue values to properties.
To see how the "accessor method" works, try the following example, in which a class "testclas2" is created with two properties (without value), and an accessor function that assigns value to these properties.
- Open a new document with "ActionScript 3.0 Class", give it the name "testClas2". Delete the initial code and copy the following code, then save the file, with the name "testClas2.as", in the same folder as the FLA document is.
package { // define the class public class testClas2 { // Defining properties without value public var prop1:Number; public var prop2:String; // Create accessor method public function setProp(prop1:Number, prop2:String) { // Assign values to the properties (the ones with "this") this.prop1 = prop1; this.prop2 = prop2; } } }- The function's parameters (in this case "setClas(prop1, prop2)") can have any name, but setting their name as the properties they represent helps you later to know their role and easier to understand the code.
// Create object instance of the testClas2 class var obj4:testClas2 = new testClas2(); // Verify in Output 'prop1' and 'prop2' trace(obj4.prop1+"-"+obj4.prop2); // NaN-null // Call the accessor method obj4.setProp(8, 'Tutorials'); // Verify 'prop1' and 'prop2' again trace(obj4.prop1+"-"+obj4.prop2); // 8-Tutorials- "NaN" is returned by a Number type variable without value, "null" by a String type variable without value.
This method is usefull when you want the properties value to be dinamically defined in the script.
<table><tr> <th>Title 1</th> <th>Title 2</th> </tr></table>
.some_class { line-height: 150%; }
document.getElementById("id_button").onclick = function(){ window.open("http://coursesweb.net/"); }
$ar_dir = scandir("dir_name"); var_export($ar_dir);