zoukankan      html  css  js  c++  java
  • as3的重载实现

    1:使用aruments的length属性进行重载

    代码
    function test(par1:int):void
    {
        
    if(arguments.length == 1)
            trace(
    "重载1");
        
    else if(arguments.length >1)
            trace(
    "重载2");
        
    else
            trace(
    "重载3");
    }
    test(
    1);
    test(
    1,2);
    test(
    1,2,3);
    //--------------------

    //重载1
    //重载2
    //重载2

    注意函数test在声明时只声明了 par1:int 一个参数,而在调用时程序连续调用了3次test,并且后两次参数与函数声明不符,多了。但编译器如果运行在标准模式下编译,是不会报错也可以运行的,这样arguments数组会扩大为你传进来参数的个数,如test(1,2,3),arguments长度就是3了,程序中如果需要用到2、3,只能通过 arguments[1]/[2]来找到多出的参数。用arguments时,注意编译器一定要运行在标准模式下;参数只能比函数声明多,不能少。

    2:使用 ...(args) 来传入不同参数。

    注意:...(args) ,前面是3点,英文模式下的3点,不多不少。args是传进来参数数组的名称,可以任意。

    代码
    function test(...args):void
    {
        
    if(args.length == 1)
            trace(
    "重载1");
        
    else if(args.length >1)
            trace(
    "重载2");
        
    else
            trace(
    "重载3");
    }
    test(
    1);
    test(
    1,2);
    test(
    1,2,3);

    //--------------------

    //重载1
    //重载2
    //重载2

    当使用...args ,在程序中就不能用arguments来获取参数了,取而代之是args。...args 也可以与声明参数一起使用

    代码
    function test(par1:int,...args):void
    {
        
    if(args.length == 1)
            trace(
    "参数" + par1 + "  重载1  args参数:" + args);
        
    else if(args.length == 2)
            trace(
    "参数" + par1 + "  重载2  args参数:" + args);
        
    else
            trace(
    "参数" + par1 + "  重载3  args参数:" + args);
    }
    test(
    1,1);
    test(
    1,2,3);
    test(
    1,2,3,4,5);

    //---------------------

    //参数1  重载1  args参数:1
    //参数1  重载2  args参数:2,3
    //参数1  重载3  args参数:2,3,4,5

    这样也变通模拟了函数重载,args是不需要编译器标准的无论在严谨或标准模式都可运行;args使用后arguments不能使用包括其属性;在给已声明参数赋值后,接下来无论多少个参数编译器都会将其作为数组传给args。

  • 相关阅读:
    Running ASP.NET Applications in Debian and Ubuntu using XSP and Mono
    .net extjs 封装
    ext direct spring
    install ubuntu tweak on ubuntu lts 10.04,this software is created by zhouding
    redis cookbook
    aptana eclipse plugin install on sts
    ubuntu open folderpath on terminal
    ubuntu install pae for the 32bit system 4g limited issue
    EXT Designer 正式版延长使用脚本
    用 Vagrant 快速建立開發環境
  • 原文地址:https://www.cnblogs.com/sevenyuan/p/1615540.html
Copyright © 2011-2022 走看看