积少成多Flash(2) - ActionScript 3.0 基础之包、类、包外类、命名空间、属性、方法、接口和继承-阿里云开发者社区

开发者社区> 技术小胖子> 正文

积少成多Flash(2) - ActionScript 3.0 基础之包、类、包外类、命名空间、属性、方法、接口和继承

简介:
+关注继续查看
[索引页]
[源码下载]


积少成多Flash(2) - ActionScript 3.0 基础之包、类、包外类、命名空间、属性、方法、接口和继承


作者:webabcd


介绍
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
package actionScript 

        import flash.display.Sprite; 
         
        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
package actionScript 

        import flash.display.Sprite; 
         
        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
package actionScript 

        import flash.display.Sprite; 
         
        public class ParentTest extends Sprite 
        { 
                public function ParentTest() 
                { 
                         
                } 
                 
                // ParentTest为基类,其内定义了一个名为hello()的方法 
                public function hello(s:String):String 
                { 
                        return "hello: " + s; 
                } 
        } 
}
 
ChildTest.as
package actionScript 

        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
package actionScript 

        // 定义一个名为china的命名空间 
        // 注:actionScript目录下必须要有名为china.as的文件 
        public namespace china; 
}
 
usa.as
package actionScript 

        // 定义一个名为usa的命名空间 
        // 注:actionScript目录下必须要有名为usa.as的文件 
        public namespace usa; 
}
 
NamespaceTest.as
package actionScript 

        import flash.display.Sprite; 
         
        // 使用命名控件 
        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 
                { 
                        return "hello: " + s; 
                } 
        } 
}
 
InterfaceTest.as
package actionScript 

        // 定义一个接口,该接口有一个方法 
        public interface InterfaceTest 
        { 
                function writeLog():String; 
        } 
}
 
InterfaceTestA.as
package actionScript 

        // 实现InterfaceTest接口 
        // 在同一个包中,所以不需要import InterfaceTest 
        public class InterfaceTestA implements InterfaceTest 
        { 
                // 实现InterfaceTest接口的writeLog()方法 
                public function writeLog():String 
                { 
                        return "记录日志到SQL Server数据库"; 
                } 
        } 
}
 
InterfaceTestB.as
package actionScript 

        // 实现InterfaceTest接口 
        // 在同一个包中,所以不需要import InterfaceTest 
        public class InterfaceTestB implements InterfaceTest 
        { 
                // 实现InterfaceTest接口的writeLog()方法 
                public function writeLog():String 
                { 
                        return "记录日志到XML文件"; 
                } 
        } 
}
 
OO.as
package 

        import flash.display.Sprite; 
         
        // 导入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); 
                        // output: webabcd 
                         
                        StaticTest.age = 27; 
                         
                        trace(StaticTest.age); 
                        // output: 27 
                         
                        trace(StaticTest.hello("webabcd")); 
                        // output: hello: webabcd 
                } 
                 
                // 包外类 
                function showPackageOut() 
                { 
                        var po:PackageOut = new PackageOut(); 
                         
                        trace(po.hello("webabcd")); 
                        // output: 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")); 
                        // output: 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); 
                        // output: true 
                         
                        var b:InterfaceTest = new InterfaceTestB(); 
                         
                        trace(b.writeLog()); 
                        // output: 记录日志到XML文件 
                         
                        trace(b is InterfaceTest); 
                        // output: true 
                } 
        } 


// 包外类(只有当前类文件中的成员类可以访问) 
class PackageOut 

        public function hello(s:String):String 
        { 
                return "hello: " + s; 
        } 
}
 
 





     本文转自webabcd 51CTO博客,原文链接:http://blog.51cto.com/webabcd/342181,如需转载请自行联系原作者


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Java基础-23总结多线程,线程实现Runnable接口,线程名字获取和设置,线程控制,线程安全,同步线程
你需要的是什么,直接评论留言。 获取更多资源加微信公众号“Java帮帮” (是公众号,不是微信好友哦) 还有“Java帮帮”今日头条号,技术文章与新闻,每日更新,欢迎阅读 学习交流请加Java帮帮交流QQ群553841695 分享是一种美德,分享更快乐! 1:多线程(理解) (1)多线程:一个应用程序有多条执行路径 进程:正
2757 0
重磅|阿里云HBase Ganos全新升级,推空间、时空、遥感一体化基础云服务
Ganos是阿里云时空PaaS服务的自研核心引擎。Ganos已作为云数据库时空引擎与数据库平台融合,建立了以自研云原生数据库POALRDB为基础,联合NoSQL大数据平台(Ali-HBASE和X-Pack Spark)的完整时空地理信息云化管理解决方案。
2056 0
HBase运维基础——元数据逆向修复原理
鉴于上次一篇文章——“云HBase小组成功抢救某公司自建HBase集群,挽救30+T数据”的读者反馈,对HBase的逆向工程比较感兴趣,并咨询如何使用相应工具进行运维等等。总的来说,就是想更深层理解HBase运维原理,提高运维HBase生产环境的能力,应对各种常见异常现象。
6366 0
java-基础-接口和抽象类
 对于面向对象编程来说,抽象是它的一大特征之一。在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类。这两者有太多相似的地方,又有太多不同的地方。很多人在初学的时候会以为它们可以随意互换使用,但是实际则不然。
744 0
155个JavaScript基础问题(31-40)
155个JavaScript基础问题(31-40)
26 0
Spring 框架基础(03):核心思想 IOC 说明,案例演示
本文源码:GitHub·点这里 || GitEE·点这里 一、IOC控制反转 1、IOC容器思想 Java系统中对象耦合关系十分复杂,系统的各模块之间依赖,微服务模块之间的相互调用请求,都是这个道理。
1982 0
oracle查找索引及表的其它属性
1、查找表的所有索引(包括索引名,类型,构成列):select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.
468 0
13668
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
文娱运维技术
立即下载
《SaaS模式云原生数据仓库应用场景实践》
立即下载
《看见新力量:二》电子书
立即下载