Flashmx2004系列教程〈三〉ActionScript2.0-AS1.0中的一点点面向对象编程
更新时间:
AS1中的一点点面向对象编程
在我们转移到AS2之前先让我了解一下在AS1时的面向对象的编程。这一节对于在FLASH5和FLASHMX不太了解面向对编程的人来说很重要。如果你已经很了解这些可以直接跳过此节。
尽管AS1不是真正的面向对象的编程语言,开发人员已经在有些时候使用它进行面向对象的编程了。AS1中的任何东西都是依靠原型链也就是对象之间的联系。所以在AS1中使用面向对象需要了解原型链(或者是原型的关键字)。
AS1的类就象是规则的函数。方法附加在这个的类的原型上。例如:
// Wizard class
function Wizard() {
}// help()方法附加在WIZARD函数的原型上。 Wizard.prototype.help = function() { };如果我们把help()直接放在wizard class类中。FLASH在查找属性和方法时就不会找到它,因为FLASH在查找是沿着原型链进行搜索的。而在为所有的Wizard类创建一个实例copy.下面就是为每个实例创建的copy.function Wizard(){this.help=function(){}}对于java,c#的程序员来说。这样的将方法代码放在类中会看来很熟悉,然而为了代码的可重用性我们还是应将方法附加在类的原型链上。在下面的例子中假如我们针对一个类上有两个方法,一个是附加在原型链上,另一个是直接放在类中,flash将先获得内部方法。// AS1_OOP_01.fla function TestClass() { this.method = function() { trace("Internal method"); };
this.prop = ">>> Internal prop"; }// Attach a method to the prototype object of the class TestClass.prototype.method = function() { trace("Prototype method"); };TestClass.prototype.prop = ">>> Prototype prop";// Create an instance of the TestClass class var w = new TestClass();// Internal method is located before the prototype method w.method();// Replace the Internal method w.method = function() { trace("New method"); }; w.method();// Delete the Internal method delete w.method;// The only method remaining is the prototype method w.method();// Test the properties trace(w.prop);w.prop = ">>> New prop"; trace(w.prop);delete w.prop; trace(w.prop);上面的例子的输出内容为:从上面这个例子我们可以看出在使用AS1面向对向对于初学者来说是极易混淆的。其中知道将代码写在什么位是很重要的,因为它可能经常出现意想不到的结果,正如在商业上有一句流行的话是:但是等等,还有更多。。。。
栏目精选
相关文章
中"Fl,la,as,sh,hm,mx,x2,20,00,04,4系,系列,列教,教程,程〈,〈三,三〉,〉A,Ac,ct,ti,io,on,nS,Sc,cr,ri,ip,pt,t2,2.,.0,0-,-A,AS,S1,1.,.0,0中,中的,的一,一点,点点,点面,面向,向对,对象,象编"相关内容
中"Fl,la,as,sh,hm,mx,x2,20,00,04,4系,系列,列教,教程,程〈,〈三,三〉,〉A,Ac,ct,ti,io,on,nS,Sc,cr,ri,ip,pt,t2,2.,.0,0-,-A,AS,S1,1.,.0,0中,中的,的一,一点,点点,点面,面向,向对,对象,象编"相关内容
中"Fl,la,as,sh,hm,mx,x2,20,00,04,4系,系列,列教,教程,程〈,〈三,三〉,〉A,Ac,ct,ti,io,on,nS,Sc,cr,ri,ip,pt,t2,2.,.0,0-,-A,AS,S1,1.,.0,0中,中的,的一,一点,点点,点面,面向,向对,对象,象编"相关内容
每日推荐











