ActionScript Syntax for Flex Applications - Variables and Properties continued
(Page 4 of 4 )
In addition to thepublic,private,protected, andinternalmodifiers, you can also combine these modifiers with thestaticmodifier. Thestatic modifier says that the property is directly accessible from the class rather than from instances. Static modifiers are used for many purposes, including design patterns such as the Singleton pattern. The following example adds a staticprivateproperty called
_instanceof typeExample:
package com.example {
import flash.net.URLLoader;
import flash.net.URLRequest;
public class Example {
private var _loader:URLLoader;
static private var _instance:Example;
}
}
A concept related to properties is that of the constant. A constant is a container for data, much like a variable/property except that once it has a value, you cannot change the value (hence the name, constant). You’ve likely seen constants in the Flash Player and Flex framework API. A few examples of constants areEvent.COMPLETE,MouseEvent.CLICK,TimerEvent.TIMER, andMath.PI. Although not a requirement, most constants are declared asstatic, and most are also declared aspublic(unlike properties, constants aren’t part of a class’s state and can therefore be declaredpublic). To declare a constant, use theconst keyword rather thanvar. By convention, constant names are all uppercase, as shown here:
package com.example{
import flash.net.URLLoader;
import flash.net.URLRequest;
public class Example {
private var _loader:URLLoader;
static private var _instance:Example;
static public const TEST:String = "test constant";
}
}
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Check it out today at your favorite bookstore. Buy this book now.
|
|