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

来源:互联网 发布:国际原油看盘软件 编辑:程序博客网 时间:2024/05/22 15:05
[源码下载]


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


作者: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(
300100));
            
// output: 200
            
            trace(ft.Add(
"webabcd"12345));
            
// 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;
    }

}


OK
[源码下载]
原创粉丝点击