Actionscript 3 - First Steps XI

      FLASH 2005-10-19 22:32
Actionscript 2 wasn't that bad after all, but a few things drove some developers crazy. For example if you initialized a member variable with an Object, this member magically behaved like a static classmember, due to the storage of the member in the prototype. In ActionScript 3 this works just like expected, as you can see here.
//////////
在AS2中的老问题,在AS3中得到解决了。以内联方式初始化数组和对象,属性可能变成静态成员。(可以参看LUAR的书《ACTIONSCRIPT 2.0与RIA应用程序开发》80页)

在AS2里面
Test1.as
import Test2;
class Test1 extends MovieClip
{
	public function Test1 ()
	{
		var t1:Test2 = new Test2 ();
		var t2:Test2 = new Test2 ();
	}
}

Test2.as
class Test2
{
	private var a:Array = new Array ();
	function Test2 ()
	{
		a.push (a.length);
		trace (a);
	}
}

数组变成静态成员,与预想的结果不一致

0
0,1


在AS3中
package {
	
	import flash.util.trace;
	import flash.display.MovieClip;

	public class Test1 extends MovieClip {
		
		public function Test1() {
			var t1:Test2 = new Test2();
			var t2:Test2 = new Test2();
		}
		
	}
	
	private class Test2 {
		
		private var a:Array = new Array();
		
		public function Test2() {
			a.push( a.length);
			trace(a);	
		}
		
	}
	
}

输出正常的结果

0
0
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}