Flash as 3.0 Novice Learning to fly Tutorial: Creating a Custom class

Source: Internet
Author: User
Tags modifiers root directory

In ActionScript 3, classes are the most basic programming structure, so you must first master the basics of writing classes. All classes must be placed in the. as file, and only one public class can be defined in each as file, and the class name must be the same as the file name. For example: Your class name is example, then the filename must be example.as.

All classes in ActionScript 3.0 must be placed in the package. A package is a unit of classification for a class, which is equivalent to a directory of file systems. The package path is relative to the classpath (Classpath), and the default classpath is the root of the project, so the top-level package directory is the project root directory. The package declaration is as follows:

package name {
}

If the class is defined in a top-level package, the package name may not be specified, such as:

package {
}

When a class file is saved in a subdirectory, the package name is its saved directory, for example, the file is saved in the example directory, so the package declares:

package example {
}

If the class file is saved in a subdirectory of the example directory Subpackage, declare this:

package example.subpackage {
}

Packages are important and can avoid class namespace collisions. For example, two developers have written two class files called MessageManager. These two classes have the same name but do different tasks, so you can't put these two classes together, and if you do, the compiler will not know which one to call, and the other is to take a unique class name.

You can name Emailmanager and Binarysocket-messagemanager, which is fine, but it's difficult if you manage thousands of classes. So using a package can be a good solution to this problem, even if you have many of the same class name, as long as they are not in the same package will not conflict, such as the MessageManager placed in the Net.messaging.email The package is placed in another Net.messaging.binarysocket package.

Generally take the package names to their own site domain name, so as to maximize the avoidance of other people's package name conflict.

When multiple projects are common to some classes, these classes are placed directly in subdirectories in the main package. For example, the MessageManager class above is placed in the Com.examplecorp.net.messaging.email and Com.examplecorp.net.messaging.binary-socket packages.

The next step is to declare the class itself:

public class Name {
}

The class declaration must be within the package. The following code defines a class called example in the top-level package:

package {
public class Example {
}
}

Class bodies are defined in parentheses, including properties, methods. A property is a variable associated with a class, declares them with the Var keyword, and a property with a modifier that specifies its scope. Modifiers are:

Private

This property is accessible only to the class instance itself.

Public

This property can be accessed by any class instance (if it can be set to static directly by class access)

Protected

This property is accessed only by its own class instance or by a derived class instance.

Internal

This property can be accessed by the class instance within the package.

By default, properties are specified as internal unless you specify modifiers yourself. In most cases, attributes are specified as private or protected. According to custom conventions, property names for private and protected declarations are preceded by an underscore.

Look at the following example:

package {
public class Example {
private var _id:String;
}
}

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.