TSL语言基础 > 高级语言(新一代) > 对象对基础算符与基础函数的重载 > TSL对象支持对二进制函数的重载 > 二进制函数在TSL对象中的重载

1、使用类方法实现二进制函数重载的示例    

  • 现有整数类IntDate,代码如下:
    type IntDate=class
      value;
       function create(v)
      begin
        value:=v;
      end
      class function operator DateToStr(t)
     begin
    t:=ifObj(t)?t.value:t;
        endt:=IntToDate(t);
        return DateToStr(endt);
     end
    end

    其中,创建了类方法DateToStr,实现了对二进制函数DateToStr的重载。
    在调用时,可以使用如下两种方式:
    d:=new IntDate(20240329);
    return DateToStr(d); //方式一
    return class(IntDate).DateToStr(20240329); //方式二

    结果:返回字符串"2024-03-29"