Instance and class members
61
Class members are declared with the
static
modifier. For example, you could declare the
species
class member with the following code:
class Person
{
static var species:String = "Homo sapiens";
...
}
You can also declare methods of a class to be static, as shown in the following code:
static function
functionName
() {
// function body
}
Class (static) methods can access only class (static) properties, not instance properties. For
example, the following code will result in a compiler error because the class method
getName()
references the instance variable
name
:
class StaticTest {
var name:String="Ted";
static function getName():Void {
var local_name:String = name;
// Error! Instance variables cannot be accessed in static functions.
}
}
To solve this problem, you could either make the method an instance method or make the
variable a class variable.
A common use of class members is the
Singleton design pattern
. The Singleton design pattern
makes sure that a class has only one instance and provides a way of globally accessing the instance.
For more information on the Singleton design pattern, see
www.macromedia.com/devnet/mx/
coldfusion/articles/design_patterns.html
.
Often there are situations when you need exactly one object of a particular type in a system. For
example, in a chess game, there is only one chessboard, and in a country, there is only one capitol
city. Even though there is only one object, it is attractive to encapsulate the functionality of this
object in a class. However, you might need to manage and access the one instance of that object.
Using a global variable is one way to do this, but global variables are often not desirable. A better
approach is to make the class manage the single instance of the object itself using class members,
such as the following example:
class Singleton {
private var instance:Singleton = null;
public function doSomething():Void {
//...
}
public static function getInstance():Singleton {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Содержание FLEX
Страница 1: ...Flex ActionScript Language Reference ...
Страница 8: ......
Страница 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0 ...
Страница 76: ......
Страница 133: ...break 133 See also for for in do while while switch case continue throw try catch finally ...
Страница 135: ...case 135 See also break default strict equality switch ...
Страница 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while ...
Страница 229: ...while 229 i 3 The following result is written to the log file 0 3 6 9 12 15 18 See also do while continue for for in ...
Страница 808: ...808 Chapter 7 ActionScript for Flash ...
Страница 810: ...810 Appendix A Deprecated Flash 4 operators ...
Страница 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code ...
Страница 816: ...816 Appendix B Keyboard Keys and Key Code Values ...
Страница 822: ...822 Index ...