AS3 Package (package) Application instance code _flash AS3

Source: Internet
Author: User
Tags class definition
The concept of package (package) already exists in AS2, which represents a directory structure in the hard disk that is used to classify and store various class files. In AS3 this concept can still be understood as a path, or directory structure, where the name of the package is the directory where your class resides.

If the class file and the FLA file are stored in the same directory location, you do not need to specify a name for the package. For example: We first create such a mymsg.as class definition file, and enter the following code in the file:

Package {
public class Mymsg {
Public Function showmsg () {
Trace ("I ' m in the same place with FLA file.");
}
}
}


It then creates an instance of the object in the home scene and invokes its method program showmsg.

var msg:mymsg = new mymsg ();
Msg.showmsg ();

Save the FLA file with the class file, and then you can see that the Output window will output what we have set.

In the location of the file save, we create a directory, such as DZXZ, to build a directory in it, such as called AS3, in the DZXZ\AS3 directory to create a new class file called Mymsg2.as, which is located in a directory structure, so you need to specify its package name, the class code written in the following form:

Package DZXZ.AS3 {
public class MyMsg2 {
Public Function showmsg () {
Trace ("I ' m in the package of DZXZ.AS3!");
}
}
}

In the home scene to use the MYMSG2 class in the package, you must import the package, use the import statement, add a row of the import statement, and the code should read:

Import DZXZ.AS3.MYMSG2;

var msg:mymsg2 = new MyMsg2 ();
Msg.showmsg ();

(found in the test, if the class name in the package and the name of the class in the current directory, the current class file is used preferentially, so the class name in the package is changed to MYMSG2)

As you can see from the previous example, the package name is written between package and {, and the name mymsag.as of the class file is consistent with the class name mymsg inside it. If you use the package name, you have to use the directory structure to hold the class file, which is a good thing when you have more than one class.

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.