标准的AS3中的Class

含有多个类的类文件ToyCar.as

package sample.car 
{
	import flash.accessibility.ISearchableText;
	/**
	 * ...
	 * @author lzc
	 */
	public class ToyCar //与类文件同名的Class,对外开放。package关键字后的{}内,必须且只能定义一个class,“包外类”可定义多个
	{
		
		static var TIRESNUMBER:int = 4;
		private var _body:CarBody;
		private var _tires:Array;
		public function ToyCar() 
		{
			init();
		}
		
		private function init():void {
			_body = new CarBody();
			_tires = new Array();
			
			for (var i:int = 0; i < TIRESNUMBER; i++ ) {
				_tires[i] = new CarTire();
			}
		}
		
		
		public function run():void {
			trace("run");
		}
	}

}


//以下CarBody和CarTire两个类,是在package{}外面定义的,
//虽然在同一个.as文件中,但除了本文件中可被访问外,对外不可见
class CarBody {
	public function CarBody() {
		trace("Car body created");
	}
}

class CarTire {
	public function CarTire() {
		trace("One tire created!");
	}
}
写一个Document Class类来运行如下代码,Class的名为TestToyCar

package sample.car 
{
	/**
	 * ...
	 * @author ...
	 */
	
	 import flash.display.Sprite;
	 import flash.sampler.car.ToyCar;
	 
	public class TestToyCar extends Sprite
	{
		
		public function TestToyCar() 
		{
			var bb = new ToyCar();
		}
		
	}

}

运行后,在console上输入如下:

Car body created

One tire created!

One tire created!

One tire created!

One tire created!


(from Flash AS3 殿堂之路)

你可能感兴趣的:(Flash,AS3)