actionscript:Flash ActionScript 3.0(2) - 包、类、包外类、命名空间、属性、思路方法、接口和继承

  本文举例源代码或素材下载

  举例

  FunctionTest.as

package actionScript
{
  import flash.display.Sprite;
  
  public FunctionTest extends Sprite
  {
    public function FunctionTest
    {
      
    }
    
    // 减法
    public function Subtract(a:, b:):
    {
      // 参数总数
      trace(arguments.length);
      // output: 2
      
      // 第个参数
      trace(arguments[0]);
      // output: “参数 a 值”
      
      // 第 2个参数
      trace(arguments[1]);
      // output: “参数 b 值”
      
      // 返回a - b
       a - b;
    }
    
    // 加法(args - 任意多参数)
    public function Add(s:String, args):String
    {
      var i: = 0;
      
      // 枚举出 args 中所有参数
      for each(var v in args)
      {
        i v;
      }
      
       s + ": " + i;
    }
  }
}


  PropertyTest.as

package actionScript
{
  import flash.display.Sprite;
  
  public PropertyTest extends Sprite
  {
    // 属性
    public var nickname:String;
    public var age:;
    
    private var _salary:;
    
    public function PropertyTest
    {
      
    }
    
    // getter思路方法
    public function get salary:
    {
       this._salary;
    }
    
    // ter思路方法
    public function salary(s:):void
    {
      this._salary = s;
    }
  }
}


  StaticTest.as

package actionScript
{
  import flash.display.Sprite;
  
  public StaticTest extends Sprite
  {
    // 静态属性
    public const nickname:String = "webabcd";
    public var age:;
    
    public function StaticTest
    {
      
    }
    
    // 静态思路方法
    public function hello(s:String):String
    {
       "hello: " + s;
    }
  }
}


  ParentTest.as

package actionScript
{
  import flash.display.Sprite;
  
  public ParentTest extends Sprite
  {
    public function ParentTest
    {
      
    }
    
    // ParentTest为基类其内定义了个名为hello思路方法
    public function hello(s:String):String
    {
       "hello: " + s;
    }
  }
}


  ChildTest.as

package actionScript
{
  import actionScript.ParentTest;
  
  // ChildTest类继承自ParentTest类
  // final代表禁止继承
  public final ChildTest extends ParentTest
  {
    public function ChildTest
    {
      
    }
    
    // 重写基类(ParentTest)中hello思路方法
    public override function hello(s:String):String
    {
      // super为对基类引用
       "基类hello思路方法 - " + super.hello(s) + ";子类重写后hello思路方法 - 您好: " + s;
    }
  }
}


  china.as

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


  usa.as

package actionScript
{
  // 定义个名为usa命名空间
  // 注:actionScript目录下必须要有名为usa.as文件
  public usa;
}
NamespaceTest.as
package actionScript
{
  import flash.display.Sprite;
  
  // 使用命名Control控件
  use china;
  use usa;
  
  public NamespaceTest extends Sprite
  {
      
    public function NamespaceTest
    {
      
    }
    
    // china命名空间hello思路方法
    china function hello(s:String):String
    {
       "您好: " + s;
    }
    
    // usa命名空间hello思路方法
    usa function hello(s:String):String
    {
       "hello: " + s;
    }
  }
}
InterfaceTest.as

package actionScript
{
  // 定义个接口该接口有个思路方法
  public erface InterfaceTest
  {
    function writeLog:String;
  }
}


  InterfaceTestA.as

package actionScript
{
  // 实现InterfaceTest接口
  // 在同个包中所以不需要import InterfaceTest
  public InterfaceTestA implements InterfaceTest
  {
    // 实现InterfaceTest接口writeLog思路方法
    public function writeLog:String
    {
       "记录日志到SQL Server数据库";
    }
  }
}


  InterfaceTestB.as

package actionScript
{
  // 实现InterfaceTest接口
  // 在同个包中所以不需要import InterfaceTest
  public InterfaceTestB implements InterfaceTest
  {
    // 实现InterfaceTest接口writeLog思路方法
    public function writeLog:String
    {
       "记录日志到XML文件";
    }
  }
}


  OO.as

package
{
  import flash.display.Sprite;
  
  // 导入actionScript目录下所有包
  import actionScript.*;
  
  public OO extends Sprite
  {
    public function OO
    {
      // ernal - 包内访问(默认)
      // public - 完全公开
      // private - 仅当前类可访问
      // protected - 子类可访问
      
      // 用表达式定义
      var hello:Function = function(s:String):String
      {
         "hello: " + s;
      }
      trace(hello("webabcd"));
      // output: hello: webabcd
      
      // 思路方法
      showFunction;
      
      // 属性、getter思路方法和ter思路方法
      showProperty;
      
      // 静态属性和静态思路方法
      showStatic;
      
      // 包外类
      showPackageOut;
      
      // 命名空间
      showNamespace;
      
      // 继承、基类和子类
      showInherit;
      
      // 接口
      showInterface;
    }
    
    // 思路方法
    function showFunction:void
    {
      var ft:FunctionTest = FunctionTest;
      
      trace(ft.Subtract(300, 100));
      // output: 200
      
      trace(ft.Add("webabcd", 1, 2, 3, 4, 5));
      // output: webabcd: 15
    }
    
    // 属性、getter思路方法和ter思路方法
    function showProperty:void
    {
      var pt:PropertyTest = 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 = PackageOut;
      
      trace(po.hello("webabcd"));
      // output: hello: webabcd
    }
    
    // 命名空间
    function showNamespace
    {
      // 使用命名空间
      // use 不受上下文影响编译时会被自动地提到前端
      use china;
      
      var n:NamespaceTest = NamespaceTest;
      
      trace(n.hello("webabcd"));
      // output: 您好: webabcd
      
      // 使用命名空间名称限定符
      trace(n.usa::hello("webabcd"));
      // output: hello: webabcd
    }
    
    // 继承、基类和子类
    function showInherit
    {
      var ct:ChildTest = ChildTest;
      
      trace(ct.hello("webabcd"));
      // output: 基类hello思路方法 - hello: webabcd;子类重写后hello思路方法 - 您好: webabcd
  
      trace(ct is ParentTest);
      // output: true
    }
    
    // 接口
    function showInterface
    {
      var a:InterfaceTest = InterfaceTestA;
      
      trace(a.writeLog);
      // output: 记录日志到SQL Server数据库
      
      trace(a is InterfaceTest);
      // output: true
      
      var b:InterfaceTest = InterfaceTestB;
      
      trace(b.writeLog);
      // output: 记录日志到XML文件
      
      trace(b is InterfaceTest);
      // output: true
    }
  }
}
  
// 包外类(只有当前类文件中成员类可以访问)
PackageOut
{
  public function hello(s:String):String
  {
     "hello: " + s;
  }
}




  OK



Tags:  actionscript错误 actionscript3 actionscript3.0 actionscript

延伸阅读

最新评论

发表评论