从基础开始深入学Flash AS3教程(7)(译文)
package {
class MyClass {
function MyClass() {
var helper:MyHelper = new MyHelper();
}
}
}
class MyHelper {
function MyHelper() {
var helper:HelpersHelper = new HelpersHelper();
}
}
class HelpersHelper {
function HelpersHelper () {
}
}
public
protected
private
internal (默认)
package {
import flash.display.Sprite;
// 应用类必须定义成public (默认是internal)
public class AccessControl extends Sprite {
// 构造器总是public的
function AccessControl() {
// 只有在这个文件中才能访问辅助类
var helper:Helper = new Helper();
trace(helper.pubNum); // OK
// trace(helper.protNum); // Error - 错误,不能访问protected成员
// trace(helper.privNum); // Error - 错误,不能访问private成员
trace(helper.interNum); // OK
}
}
}
// 辅助类默认是internal
class Helper {
// public声明允许在任何地方被访问
// 变量通常被设置为private的,然后通过声明public的get set方法来访问
public var pubNum:Number = 1;
// protected声明只允许在子类中被访问
protected var protNum:Number = 2;
// private声明只能在类内部被访问
private var privNum:Number = 3;
// internal声明只允许在同一个包中被访问
// 但是对于辅助类来说,只能在同一个文件类被访问
internal var interNum:Number = 4;
// 构造器永远都是public的
function Helper() {
}
}
package {
import flash.display.Sprite;
import containers.*;
// Application class needs to be public (internal by default)
public class AccessControl extends Sprite {
// constructors are always public
function AccessControl() {
// can access classes in other packages
// only if public
var bowl:Bowl = new Bowl(); // OK
// var basket:Basket = new Basket(); // Error - cannot access internal
trace(bowl.pubNum); // OK
// trace(bowl.protNum); // Error - cannot access protected
// trace(bowl.privNum); // Error - cannot access private
// trace(bowl.interNum); // Error - cannot access internal
}
}
}
ActionScript Code:
package containers {
// public class accessible anywhere
public class Bowl {
// public access granted anywhere
public var pubNum:Number = 1;
// protected access granted only for
// subclasses in that class
protected var protNum:Number = 2;
// private access granted only in this class
private var privNum:Number = 3;
// internal access granted only in the same package
internal var interNum:Number = 4;
// constructors are always public
function Bowl() {
// can access inteneral classes if in same package
var basket:Basket = new Basket();
trace(basket.pubNum); // OK
// trace(basket.protNum); // Error - cannot access protected
// trace(basket.privNum); // Error - cannot access private
trace(basket.interNum); // OK - same package
// clone using public method
var basketCopy:Basket = basket.clone();
}
}
}
ActionScript Code:
package containers {
// interal only accessible
// from other classes in package
internal class Basket {
// public access granted anywhere
public var pubNum:Number = 1;
// protected access granted only for
// subclasses in that class
protected var protNum:Number = 2;
// private access granted only in this class
private var privNum:Number = 3;
// internal access granted only in the same package
internal var interNum:Number = 4;
// constructors are always public
function Basket() {
}
// accessible anywhere as long as
// referencing a Basket instance
public function clone():Basket {
var basket:Basket = new Basket();
basket.pubNum = pubNum; // OK
basket.protNum = protNum; // OK - same class
basket.privNum = privNum; // OK - same class
basket.interNum = interNum; // OK
return basket;
}
}
}
DisplayObject
InteractiveObject
DisplayObjectContainer
package {
import flash.display.DisplayObject;
public class MyDisplay extends DisplayObject{
public function MyDisplay (){
// ERROR
}
}
}