TSL语言基础 > 高级语言(新一代) > 对象对基础算符与基础函数的重载 > TSL对象对遍历算符的重载

::,:.,mcell,mrow,mcol,mIndexCount,mIndex等算符在对象中的重载    

  • 定义:function operator KeyWord([p1[,p2[,…]]]);
    说明:
    Operator:为重载关键字
    KeyWord:表示关键字函数,如::,:.,mcell,mrow,mcol,mIndexCount,mIndex等
    p1,p2,…:函数参数列表。

    其中,在::与:.遍历过程中,有一个参数,该参数为0时表示第一次循环,为1时表示非第一次循环。
    循环过程中,返回0或nil表示循环结束,非0数字则表示循环继续。

    重载示例(模拟相关算符与关键字在数组中的功能):
    type c1=class
    public
     data;
     Rdata;
     findex;
     lengtD;
     function create(v);
     begin
      data:=v;
     end;
     function operator ::(flag);//flag:0表示第一次循环;1表示非第一次循环 ,最多支持二维
     begin
      // echo "::flag ",flag;
      if not flag then //循环开始-初始化-第一次
      begin
        Rdata:=array();
        k:=0;
        data::begin
          Rdata[k]:=array(mcell,mIndexCount,mrow,mcol);
          if mIndexCount>2 then for i:=2 to mIndexCount-1 do Rdata[k,i+2]:=mIndex(i);
          k++;
         end
        lengtD:=length(Rdata);
        findex:=0;
      end
      else if findex<lengtD-1 then findex++; //循环过程
      else return nil;//循环结束
      return 1;//返回0或nil循环结束,其它数值则循环继续
     end;
     function operator :.(flag);//flag:0表示第一次循环;1表示非第一次循环 ,深度遍历
     begin
     // echo ":.flag ",flag;
      if not flag then //循环开始-初始化-第一次
      begin
        Rdata:=array();
        k:=0;
        data:.begin
          Rdata[k]:=array(mcell,mIndexCount,mrow,mcol);
          if mIndexCount>2 then for i:=2 to mIndexCount-1 do Rdata[k,i+2]:=mIndex(i);
          k++;
         end
        lengtD:=length(Rdata);
        findex:=0;
      end
      else if findex<lengtD-1 then findex++; //循环过程
      else return nil;//循环结束
      return 1;//返回0或nil循环结束,其它数值则循环继续
     end;
     function operator mcell();//单元值
     begin
      return Rdata[findex][0];
     end;
     function operator mIndexCount(); //维度数
     begin
      return Rdata[findex][1];
     end;
     function operator mrow();//单元行下标
     begin
      return Rdata[findex][2];
     end;
     function operator mcol();//单元列下标
     begin
      return Rdata[findex][3];
     end;
     function operator mIndex(n);//单元列下标
     begin
      if n<Rdata[findex][1] then
       return Rdata[findex][n+2];
      else raise "指定的维度超出最大维度数";
     end;

    end;

    调用示例:
    t:=array("A":0->3,"B":10->2,"C":20->21,"D":30->23);//不完全矩阵
     t["B",1]:=array(801,802,803); //多维矩阵
      obj:=new c1(t);
      echo "::遍历";
      obj::begin
        s:="mcell:"$mcell$" mrow:"$mrow$" mcol:"$mcol$" mIndexCount:"$mIndexCount;
        for i:=0 to mIndexCount-1 do
         s+=" mIndex("$i$"):"$mIndex(0);
        echo s;
      end
      echo ":.深度遍历";
      obj:.begin
        s:="mcell:"$mcell$" mrow:"$mrow$" mcol:"$mcol$" mIndexCount:"$mIndexCount;
        for i:=0 to mIndexCount-1 do
         s+=" mIndex("$i$"):"$mIndex(0);
        echo s;
      end
      return 1;

    打印结果:
    ::遍历
    mcell:0 mrow:A mcol:0 mIndexCount:2 mIndex(0):A mIndex(1):A
    mcell:1 mrow:A mcol:1 mIndexCount:2 mIndex(0):A mIndex(1):A
    mcell:2 mrow:A mcol:2 mIndexCount:2 mIndex(0):A mIndex(1):A
    mcell:3 mrow:A mcol:3 mIndexCount:2 mIndex(0):A mIndex(1):A
    mcell: mrow:B mcol:1 mIndexCount:2 mIndex(0):B mIndex(1):B
    mcell:20 mrow:C mcol:0 mIndexCount:2 mIndex(0):C mIndex(1):C
    mcell:21 mrow:C mcol:1 mIndexCount:2 mIndex(0):C mIndex(1):C
    :.深度遍历
    mcell:0 mrow:A mcol:0 mIndexCount:2 mIndex(0):A mIndex(1):A
    mcell:1 mrow:A mcol:1 mIndexCount:2 mIndex(0):A mIndex(1):A
    mcell:2 mrow:A mcol:2 mIndexCount:2 mIndex(0):A mIndex(1):A
    mcell:3 mrow:A mcol:3 mIndexCount:2 mIndex(0):A mIndex(1):A
    mcell:801 mrow:B mcol:1 mIndexCount:3 mIndex(0):B mIndex(1):B mIndex(2):B
    mcell:802 mrow:B mcol:1 mIndexCount:3 mIndex(0):B mIndex(1):B mIndex(2):B
    mcell:803 mrow:B mcol:1 mIndexCount:3 mIndex(0):B mIndex(1):B mIndex(2):B
    mcell:20 mrow:C mcol:0 mIndexCount:2 mIndex(0):C mIndex(1):C
    mcell:21 mrow:C mcol:1 mIndexCount:2 mIndex(0):C mIndex(1):C