Flash / Flex / ActionScript/Language/package
Содержание
- 1 A package-level function
- 2 Global Scope: Code placed directly outside a package body or at the top-level of a package body resides in the global scope.
- 3 Import all the classes in a particular package by using the wildcard operator (*) trailing the package name
- 4 package level function
- 5 Packages groups classes
- 6 References to the classes by using their full name
- 7 The general form of a package definition directive
- 8 To gain access to all the public classes in another package.
- 9 Variables, functions, and namespace declarations can also be contained inside packages.
A package-level function
package com.example.shape
{
public function testShapes():void
{
trace("test the shapes here...");
}
}
Global Scope: Code placed directly outside a package body or at the top-level of a package body resides in the global scope.
package {
// Code here is in the global scope
}
// Code here is also in the global scope
package {
// Definitions here are accessible to all code in the global scope
}
// Definitions here are accessible to all code in the same source file
Import all the classes in a particular package by using the wildcard operator (*) trailing the package name
package
{
import com.example.shapes.*;
public class PackagesTest
{
public function PackagesTest()
{
var r:Rectangle = new Rectangle();
10 + DEFAULT_SIZE;
testShapes();
}
}
}
package com.example.shapes
{
public const DEFAULT_SIZE:int = 256;
}
package com.example.shapes
{
public class Rectangle
{
// define Rectangle here.
}
}
package level function
package {
import flash.display.Sprite;
public class CallPackagedFunction extends Sprite {
public function CallPackagedFunction() {
PackagedFunction(graphics);
}
}
}
package {
import flash.display.Graphics;
public function PackagedFunction(target:Graphics):void {
target.lineStyle(0);
target.lineTo(100,100);
}
}
Packages groups classes
package com.example.shapes
{
public class Rectangle
{
// define Rectangle here.
}
}
class SecretClass
{
// define SecretClass here.
}
References to the classes by using their full name
package
{
import com.example.shapes.Rectangle;
import flash.geom.Rectangle;
public class PackagesTest
{
public function PackagesTest()
{
var a:com.example.shapes.Rectangle =
new com.example.shapes.Rectangle();
var b:flash.geom.Rectangle =
new flash.geom.Rectangle();
}
}
}
package com.example.shapes
{
public class Rectangle
{
// define Rectangle here.
}
}
The general form of a package definition directive
package packageName {
}
To add a class to a package
package packageName {
Class source code goes here
}
To create a new class: use a class definition
class Identifier {
}
package com.wbex {
class MyClass {
}
}
To gain access to all the public classes in another package.
import packageName.*
Variables, functions, and namespace declarations can also be contained inside packages.
package com.example.shapes
{
public const DEFAULT_SIZE:int = 256;
}