ActionScript 3 One of the cumulative

Source: Internet
Author: User
Tags constructor file system garbage collection inheritance variables variable scope

This article is my (AW) after sorting out the relevant documents and discussions, combined with my own experiments summed up some of the experiences and experience. I try to describe it in detail, avoid vague concepts, and of course I want all reader to comment. In order to express the convenience, the term is not qualified language, such as I may be a moment with class, a moment with "class."

The difficult part of object-oriented is the understanding of variable scope modifiers (modifier), which is the public, protected, private, and so on that we are already familiar with in object-oriented objects. This paper also discusses in depth the new concepts of internal and so on in ACTIONSCRIPT3. I'll list the following in turn:

I. About package and internal
Package, in a "metaphysical" way, is the collection of classes in the physical directory. In AS2, you only need to ensure that the file system path matching, and then use a similar "import Com.awflasher.someUtils" method to import. AS3, however, requires that you declare package keywords in all classes. Package braces for "{}", we can only define a class, and we could define some auxiliary classes outside of the curly braces, but these classes can only be accessed by the current class (the class you defined internally in package braces). Of course, a package curly brace has only one class inside, which does not mean that there is only one class within a package. You can define multiple classes in the same directory that belong to the package (refer to this directory). It's not just a simple "class file collection container", but a package that lets a variety of classes that should work together. It is worth mentioning that the so-called "collaborative work" refers to at least one class to introduce a number of other classes for functional design, while the use of internal modification can save a lot of getters and setters. I recall that in Hunan TV project with AS2 developed Vplayer, two classes Avcore and Avcontrol have a lot of getter and setter, make the special trouble.

Internal is similar to public, but is limited to a package. Classes within the same package can access internal variables of other classes within the same package, while classes within other packages are inaccessible. Package has nothing to do with the inheritance of classes, such as TextField and Sprite, MovieClip inherit from Displayobject classes, but TextField belong to Flash.text packs, while MovieClip and Sprite belong to the Flahs.display package. That is, a package's qualification of a class is a new dimension with no connection to the inheritance chain.
Attachment: When using a class, we must import this class, or include the package of this class. AS2 the use of direct write full package path in the AS3 is not used, the article is described in detail later.

Ii. about Public
Public-defined classes or properties can be accessed by any source within any scope. Constructors are always public, the application class (Application Class) in Flex and the document class in Flash CS3 must be public. And you cannot default public this keyword declaration. I found in the test that if you do not declare Public,flash, you will not get the definition of the class, and then the compilation cannot pass.

Iii. about protected
The protected declaration is similar to AS2 's private, and its defined attributes can only be seen in its own subclasses, and other occasions are not visible. This is similar to traditional OOP languages such as Java.

Iv. about Private
Note that AS3 's private and AS2 's private are very different, and that the attributes it defines belong only to themselves, and subclasses can define an attribute with the same name that has no implications.
Dynamic, like the dynamic of the original AS2, classes declared with dynamic can add properties dynamically. These properties can also be deleted through the delete. Dynamically added properties are automatically reclaimed by the garbage collection mechanism once they are cut off all references. Memory deallocation is sometimes not detected with System.totalmemory because the garbage collection mechanism is not running immediately.

V. About DYNAMIC
Dynamic classes allow properties to be added dynamically at run time, and common dynamic classes have MovieClip and top-level (top-level) array. If your custom class is to inherit from a dynamic class, then define it as dynamic and do not omit the active keyword.

Vi. on Inheritance (extends) and override
Inheritance is not very complex, the only thing to explain is: the constructor of subclasses must be called "super" the constructor of the parent class, or Error! For inherited subclasses, you must use the Override keyword if you want to redefine the non-private method of the parent class. In override, if we need to invoke the method of the parent class, we can use the Super keyword (because the inheritance method is logically similar to the parent class, so there is no need to rewrite the method logic completely) This example in official help is very understandable:
Package {
Import Flash.display.MovieClip;
public class Superexample extends MovieClip
{
Public Function Superexample ()
{
var myext:extender = new Extender ()
Trace (Myext.thanks ()); Output:mahalo Nui Loa
}
}
}

Class Base {
Public Function Hi (): String
{
return "Mahalo";
}
}

Class Extender extends Base
{
Override Public Function (): String
{
return super.thanks () + "Nui Loa";
}
}

Override cannot be used with overloaded variables (member properties). But it can be used to override getter and setter functions, such as: (example of official help)
Package
{
Import Flash.display.MovieClip;
public class Overrideexample extends MovieClip
{
Public Function Overrideexample ()
{
Trace (CurrentLabel)
}
Override public Function Get CurrentLabel (): String
{
var str:string = "Override:";
str = Super.currentlabel;
return str;
}
}
}

In this example, we directly rewrite the CurrentLabel property of the MovieClip class. Note that when the parent class attribute is invoked, the Super.currentlabel is used.

About static method, more troublesome. First, a static method cannot be overloaded. Must be accessed through a class. But you can also define a method that has the same name as a static method, and I've made some changes to the official example at a glance:

Package
{
Import Flash.display.MovieClip;
public class Staticexample extends MovieClip
{
Public Function Staticexample ()
{
var myext:extender = new Extender ();
}
}
}

Class Base {
public static var test:string = "Static";
}

Class Extender extends Base
{
private var test:string = "instance";
Public Function Extender ()
{
Trace (base.test); Output:static
Trace (test); Added by awflasher.com, output:instance
}
}

Vii. About import Grammar
In the AS2 era, the "import" syntax is just to make programming code concise (Save the package name), for example, after we import the Mx.transitions.Tween, we can directly new Tween (). And if not import, we can also directly use the full class name to construct the instance, for example: New Mx.transitions.Tween ();

However, in AS3, whether or not a full name class declaration is used, you must import the class as long as you use it. Import estimation is one of the most important habits that traditional flash programmers need to develop, and I often forget to import some common classes, such as text and render components from the IDE, and flash.event.*, when I first cut into the AS3 development platform. Flash.display.StageAlign such as comparison of generic classes.

Unlike AS2 in AS3, we cannot displaystate with _root and stage["hacks".

Viii. about compile-time considerations
AS3 is no longer as simple as AS2 complie-time (when compiling, that is, when the other compilers publish ActionScript and all the resources for the SWF file) for type detection, AS3 also has type detection at Run-time (runtime, level Flashplayer, or other software that plays SWF) while playing SWF, so AS2 hacks (forced access to inaccessible properties) is no longer valid.

九、一个 had to say the good news
All of the variables (properties) and functions (methods) in AS3 are scoped to the runtime and class/instance. This is very different from AS2, we no longer need to go to delegate. As long as it is a class of its own method, in the process of invocation, this always points to the class itself (instance).

Brief summary:
1. If I need a property to be exposed and can be inherited by my subclass, then it is defined as public. Subclasses can also be overridden (override).
2, if I need attribute hiding, but can be inherited by my subclass, then it is defined as protected. Like public, subclasses can also be overridden (override).
3. If one of my classes has an attribute that does not want to be visible anywhere, including its subclasses, then it is defined as private. Its subclasses do not need to be overridden (override) because it does not exist in subclasses at all.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.