天天看點

積少成多Flash(2) - ActionScript 3.0 基礎之包、類、包外類、命名空間、屬性、方法、接口和繼承

<a href="http://webabcd.blog.51cto.com/1787395/342175" target="_blank">[索引頁]</a>

<a href="http://down.51cto.com/data/100162" target="_blank">[源碼下載下傳]</a>

積少成多Flash(2) - ActionScript 3.0 基礎之包、類、包外類、命名空間、屬性、方法、接口和繼承

介紹

Flash ActionScript 3.0 是一種面向對象的語言,包、類、包外類、命名空間、屬性、方法、接口和繼承

示例

FunctionTest.as

package actionScript 

        import flash.display.Sprite; 

        public class FunctionTest extends Sprite 

        { 

                public function FunctionTest() 

                { 

                } 

                // 減法 

                public function Subtract(a:int, b:int):int 

                        // 參數總數 

                        trace(arguments.length); 

                        // output: 2 

                        // 第一個參數 

                        trace(arguments[0]); 

                        // output: “參數 a 的值” 

                        // 第二個參數 

                        trace(arguments[1]); 

                        // output: “參數 b 的值” 

                        // 傳回a - b 

                        return a - b; 

                // 加法(args - 任意多參數) 

                public function Add(s:String, args):String 

                        var i:int = 0; 

                        // 枚舉出 args 中的所有參數 

                        for each(var v in args) 

                        { 

                                i += v; 

                        } 

                        return s + ": " + i; 

        } 

}

PropertyTest.as

        public class PropertyTest extends Sprite 

                // 屬性 

                public var nickname:String; 

                public var age:int; 

                private var _salary:int; 

                public function PropertyTest() 

                // getter方法 

                public function get salary():int 

                        return this._salary; 

                // setter方法 

                public function set salary(s:int):void 

                        this._salary = s; 

StaticTest.as

        public class StaticTest extends Sprite 

                // 靜态屬性 

                public static const nickname:String = "webabcd"; 

                public static var age:int; 

                public function StaticTest() 

                // 靜态方法 

                public static function hello(s:String):String 

                        return "hello: " + s; 

ParentTest.as

        public class ParentTest extends Sprite 

                public function ParentTest() 

                // ParentTest為基類,其内定義了一個名為hello()的方法 

                public function hello(s:String):String 

ChildTest.as

        import actionScript.ParentTest; 

        // ChildTest類繼承自ParentTest類 

        // final代表禁止繼承 

        public final class ChildTest extends ParentTest 

                public function ChildTest() 

                // 重寫基類(ParentTest)中的hello()方法 

                public override function hello(s:String):String 

                        // super為對基類的引用 

                        return "基類的hello()方法 - " + super.hello(s) + ";子類重寫後的hello()方法 - 您好: " + s; 

china.as

        // 定義一個名為china的命名空間 

        // 注:actionScript目錄下必須要有名為china.as的檔案 

        public namespace china; 

usa.as

        // 定義一個名為usa的命名空間 

        // 注:actionScript目錄下必須要有名為usa.as的檔案 

        public namespace usa; 

NamespaceTest.as

        // 使用命名控件 

        use namespace china; 

        use namespace usa; 

        public class NamespaceTest extends Sprite 

                public function NamespaceTest() 

                // china命名空間的hello()方法 

                china function hello(s:String):String 

                        return "您好: " + s; 

                // usa命名空間的hello()方法 

                usa function hello(s:String):String 

InterfaceTest.as

        // 定義一個接口,該接口有一個方法 

        public interface InterfaceTest 

                function writeLog():String; 

InterfaceTestA.as

        // 實作InterfaceTest接口 

        // 在同一個包中,是以不需要import InterfaceTest 

        public class InterfaceTestA implements InterfaceTest 

                // 實作InterfaceTest接口的writeLog()方法 

                public function writeLog():String 

                        return "記錄日志到SQL Server資料庫"; 

InterfaceTestB.as

        public class InterfaceTestB implements InterfaceTest 

                        return "記錄日志到XML檔案"; 

OO.as

package 

        // 導入actionScript目錄下的所有包 

        import actionScript.*; 

        public class OO extends Sprite 

                public function OO() 

                        // internal - 包内通路(預設) 

                        // public - 完全公開 

                        // private - 僅目前類可通路 

                        // protected - 子類可通路 

                        // 用函數表達式定義函數 

                        var hello:Function = function(s:String):String 

                                return "hello: " + s; 

                        trace(hello("webabcd")); 

                        // output: hello: webabcd 

                        // 方法 

                        showFunction(); 

                        // 屬性、getter方法和setter方法 

                        showProperty(); 

                        // 靜态屬性和靜态方法 

                        showStatic(); 

                        // 包外類 

                        showPackageOut(); 

                        // 命名空間 

                        showNamespace(); 

                        // 繼承、基類和子類 

                        showInherit(); 

                        // 接口 

                        showInterface(); 

                // 方法 

                function showFunction():void 

                        var ft:FunctionTest = new FunctionTest(); 

                        trace(ft.Subtract(300, 100)); 

                        // output: 200 

                        trace(ft.Add("webabcd", 1, 2, 3, 4, 5)); 

                        // output: webabcd: 15 

                // 屬性、getter方法和setter方法 

                function showProperty():void 

                        var pt:PropertyTest = new PropertyTest(); 

                        pt.nickname = "webabcd"; 

                        pt.age = 27; 

                        pt.salary = 1000; 

                        trace(pt.nickname); 

                        // output: webabcd 

                        trace(pt.age); 

                        // output: 27 

                        trace(pt.salary); 

                        // output: 1000 

                // 靜态屬性和靜态方法 

                function showStatic():void 

                        trace(StaticTest.nickname); 

                        StaticTest.age = 27; 

                        trace(StaticTest.age); 

                        trace(StaticTest.hello("webabcd")); 

                // 包外類 

                function showPackageOut() 

                        var po:PackageOut = new PackageOut(); 

                        trace(po.hello("webabcd")); 

                // 命名空間 

                function showNamespace() 

                        // 使用命名空間 

                        // use namespace 不受上下文的影響,編譯時會被自動地提到前端 

                        use namespace china; 

                        var n:NamespaceTest = new NamespaceTest(); 

                        trace(n.hello("webabcd")); 

                        // output: 您好: webabcd 

                        // 使用命名空間名稱限定符 

                        trace(n.usa::hello("webabcd")); 

                // 繼承、基類和子類 

                function showInherit() 

                        var ct:ChildTest = new ChildTest(); 

                        trace(ct.hello("webabcd")); 

                        // output: 基類的hello()方法 - hello: webabcd;子類重寫後的hello()方法 - 您好: webabcd 

                        trace(ct is ParentTest); 

                        // output: true 

                // 接口 

                function showInterface() 

                        var a:InterfaceTest = new InterfaceTestA(); 

                        trace(a.writeLog()); 

                        // output: 記錄日志到SQL Server資料庫 

                        trace(a is InterfaceTest); 

                        var b:InterfaceTest = new InterfaceTestB(); 

                        trace(b.writeLog()); 

                        // output: 記錄日志到XML檔案 

                        trace(b is InterfaceTest); 

// 包外類(隻有目前類檔案中的成員類可以通路) 

class PackageOut 

        public function hello(s:String):String 

                return "hello: " + s; 

OK

     本文轉自webabcd 51CTO部落格,原文連結:http://blog.51cto.com/webabcd/342181,如需轉載請自行聯系原作者