I'm learning AS3, and am a bit confused as to what a static variable or method does, or how it differs from a method or variable without this keyword. This should be simple enough to answer, I think.
标签:
actionscript-3
相关问题
- garbage collection best practices
- How to load flex swf from flash?
- FlashDevelop Haxe (Flash) debugger
- Detecting user's camera settings
- Displaying two fullscreen windows on two monitors
相关文章
- Are there any benefits when using final in AS3?
- Trace on Chrome/Browser console
- as3 ByteArray to Hex (binary hex representation)
- About Collision detection alghorithms in AS3
- How to upload BitmapData to a server (ActionScript
- Manage resources to minimize garbage collection ac
- as3 object values NativeText
- Error 1009 in AS3
A static variable or method is shared by all instances of a class. That's a pretty decent definition, but may not actually make it as clear as an example...
So in a class
Foo
maybe you'd want to have a static variablefooCounter
to keep track of how manyFoo
's have been instantiated. (We'll just ignore thread safety for now).So each time that you make a
new Foo()
in the above example, the counter gets incremented. So at any time if we want to know how manyFoo
's there are, we don't ask an instance for the value of the counter, we ask theFoo
class since that information is "static" and applies to the entireFoo
class.static
specifies that a variable, constant or method belongs to the class instead of the instances of the class.static
variable, function or constant can be accessed without creating an instance of the class i.eSomeClass.staticVar
. They are not inherited by any subclass and only classes (no interfaces) can have static members.A
static
function can not access any non-static members (variables, constants or functions) of the class and you can not usethis
orsuper
inside a static function. Here is a simple example.In the TestStatic, static variables and functions can be accessed without creating an instance of SomeClass.
Another thing is static functions could only access static variables, and couldn't be override, see "hidden".