TSL语言基础 > Object TSL > 单元中的类

单元中的类-应用实例    

  • 现有日期相关单元TD_DateUnit,单元中包含类TD_DateClass、IntDate、StrDate。
    Unit TD_DateUnit;
    Interface
    Type TD_DateClass=class()//父类
      value; //天软日期
      Function create(v);
      begin
        value := isDate(v)?v:0;
      end;
      function isDate();overload;
      begin
        return isDate(value);
      end
      class Function isDate(v);overload; //是否是一个日期
      begin
        try
          y := yearof(v);
          m := monthof(v);
          d := dayof(v);
          return isValidDate(y,m,d);
        except
          return 0;
        end;
      end;
      //--整型-日期
      Type IntDate=class(TD_DateClass)//内部类,继承父类
        iDate;//对应的整数日期
        Function create(v);
        begin
          iDate:=_datetoint(v);
          value:=inttodate(iDate);
        end;
        function _datetoint();overload;
        begin
          return iDate;
        end
        class Function _datetoint(v);overload;
        begin
          if ifstring(v)then
          begin
            _iDate := datetoint(strtodate(v));
          end else
          if v<99999 then
          begin
            isd := isDate(v);
            _iDate := isd?datetoint(v):v;
          end
          else _iDate:=Int(v);
          return _iDate;
        end
      end;
      //--字符串-日期
      Type StrDate=class()//内部类,不继承父类
        value;//天软日期
        sDate;//对应的字符串日期
        Function create(v);
        begin
          sDate:=_datetostr(v);
          value:=strtodate(sDate);
        end;
        class Function _datetostr(v)
        begin
          obj := new IntDate(v);
          _sDate := datetostr(obj.value);
          return _sDate;
        end
        Function formatS(f); //按指定符号生成字符串日期
        begin
          fs := "yyyy"+f+"mm"+f+"dd";
          return FormatDateTime(fs,value);
        end;
      end;
    End;
    Implementation
    Initialization
    Finalization End.

    使用示例
      dateClass:=findclass("TD_DateUnit.TD_DateClass");
      echo dateClass.isDate(20250912T);//天软日期,返回值:1
      echo dateClass.isDate("2025-09-12");//字符串日期,返回值:0
      //现有如下日期
      endt:=20250912T;
      //通过intDate类,转换成整数日期
      obj:=new TD_DateUnit.TD_DateClass.intDate(endt);
      echo obj.iDate;//返回值:20250912
      echo obj._datetoint("2025-09-12");//返回值:20250912
      //通过strDate类,转换成整数日期
      obj:=new TD_DateUnit.TD_DateClass.strDate(endt);
      echo obj.sDate;//返回值:2025-09-12
      echo obj._datetoStr(20250912);//返回值:2025-09-12
      echo obj.formatS(".");//返回值:2025.09.12
      return 1;

    打印结果如下: