as3.0英文帮助摘录(二)--关于namespaces
sshong 发表于2007年5月29日 17:47:00 更新于2007年5月29日 17:48:00
一、You should bear in mind the following when applying namespaces:

You can apply only one namespace to each declaration.
There is no way to apply a namespace attribute to more than one definition at a time. In other words, if you want to apply your namespace to ten different functions, you must add your namespace as an attribute to each of the ten function definitions.
If you apply a namespace, you cannot also specify an access control specifier because namespaces and access control specifiers are mutually exclusive. In other words, you cannot declare a function or property as public, private, protected, or internal in addition to applying your namespace.


二、
Referencing namespaces
There is no need to explicitly reference a namespace when you use a method or property declared with any of the access control namespaces, such as public, private, protected, and internal. This is because access to these special namespaces is controlled by context. For example, definitions placed into the private namespace are automatically available to code within the same class. For namespaces that you define, however, such context sensitivity does not exist. In order to use a method or property that you have placed into a custom namespace, you must reference the namespace.

You can reference namespaces with the use namespace directive or you can qualify the name with the namespace using the name qualifier (::) punctuator. Referencing a namespace with the use namespace directive "opens" the namespace, so that it can apply to any identifiers that are not qualified. For example, if you have defined the example1 namespace, you can access names in that namespace by using use namespace example1:

use namespace example1;
myFunction();


You can open more than one namespace at a time. Once you open a namespace with use namespace, it remains open throughout the block of code in which it was opened. There is no way to explicitly close a namespace.

Having more than one open namespace, however, increases the likelihood of name conflicts. If you prefer not to open a namespace, you can avoid the use namespace directive by qualifying the method or property name with the namespace and the name qualifier punctuator. For example, the following code shows how you can qualify the name myFunction() with the example1 namespace:

example1::myFunction();

三、The following example uses a user-defined namespace to group together two functions that reside in different packages. By grouping them into the same namespace, you can make both functions visible to a class or package through a single use namespace statement.

This example uses four files to demonstrate the technique. All of the files must be within your classpath. The first file, myInternal.as, is used to define the myInternal namespace. Because the file is in a package named example, you must place the file into a folder named example. The namespace is marked as public so that it can be imported into other packages.

// myInternal.as in folder example
package example
{
    public namespace myInternal = "http://www.adobe.com/2006/actionscript/examples";
}


The second and third files, Utility.as and Helper.as, define the classes that contain methods that should be available to other packages. The Utility class is in the example.alpha package, which means that the file should be placed inside a folder named alpha that is a subfolder of the example folder. The Helper class is in the example.beta package, which means that the file should be placed inside a folder named beta that is also a subfolder of the example folder. Both of these packages, example.alpha and example.beta, must import the namespace before using it.

// Utility.as in the example/alpha folder
package example.alpha
{
    import example.myInternal;
    
    public class Utility
    {
        private static var _taskCounter:int = 0;
    
        public static function someTask()
        {
            _taskCounter++;
        }
        
        myInternal static function get taskCounter():int
        {
            return _taskCounter;
        }
    }
}

// Helper.as in the example/beta folder
package example.beta
{
    import example.myInternal;
    
    public class Helper
    {
        private static var _timeStamp:Date;
        
        public static function someTask()
        {
            _timeStamp = new Date();
        }
        
        myInternal static function get lastCalled():Date
        {
            return _timeStamp;
        }
    }
}


The fourth file, NamespaceUseCase.as, is the main application class, and should be a sibling to the example folder. In Adobe Flash CS3 Professional, this class would be used as the document class for the FLA. The NamespaceUseCase class also imports the myInternal namespace and uses it to call the two static methods that reside in the other packages. The example uses static methods only to simplify the code. Both static and instance methods can be placed in the myInternal namespace.

// NamespaceUseCase.as
package
{
    import flash.display.MovieClip;
    import example.myInternal;       // import namespace
    import example.alpha.Utility;    // import Utility class
    import example.beta.Helper;      // import Helper class
    
    public class NamespaceUseCase extends MovieClip
    {
        public function NamespaceUseCase()
        {
            use namespace myInternal;
            
            Utility.someTask();
            Utility.someTask();
            trace(Utility.taskCounter); // 2
            
            Helper.someTask();
            trace(Helper.lastCalled);   // [time someTask() was last called]
        }
    }
}
标签:无分类:As3&Flex阅读:6066
评论
sshong2008年12月20日 20:48
已屏蔽ip:92.48.194.14。
sshong2008年12月7日 07:07
来自瑞士ip为194.165.42.59,总是发垃圾回复,已屏蔽该ip。
添加评论
您的大名,限长10汉字,20英文(*)
电子信箱(*)
您的网站
正文,限长500汉字,1000英文(*)
验证码(*) 单击刷新验证码
联系我
博客订阅