谢谢

解决方案 »

  1.   

    //这样的定义为“类型常量”~~
    {$J+}
    const
      H1: THandle = 0 ;
    //这样的编译环境中“类型常量”可以改变数值
    {$J-}
    const
      H2: THandle = 0 ;
    //这样的编译环境中“类型常量”不可以改变数值
      

  2.   

    既然可以赋值,定义为const类型有什么用,还不如直接定义为var?
      

  3.   

    function VarTypeName(const mValues: array of const): string;
    var
      vVarRecs: array[0..$FFF0 div SizeOf(TVarRec)] of TVarRec absolute mValues;
      I: Integer;
    begin
      Result := '';
      for I := Low(mValues) to High(mValues) do begin
        with vVarRecs[I] do
          case VType of
            vtInteger: begin
              Result := Result + ',' + 'vtInteger';
            end;
            vtBoolean: Result := Result + ',' + 'vtBoolean';
            vtChar: Result := Result + ',' + 'vtChar';
            vtExtended: Result := Result + ',' + 'vtExtended';
            vtString: Result := Result + ',' + 'vtString';
            vtPointer: Result := Result + ',' + 'vtPointer';
            vtPChar: Result := Result + ',' + 'vtPChar';
            vtObject: Result := Result + ',' + 'vtObject';
            vtClass: Result := Result + ',' + 'vtClass';
            vtWideChar: Result := Result + ',' + 'vtWideChar';
            vtPWideChar: Result := Result + ',' + 'vtPWideChar';
            vtAnsiString: Result := Result + ',' + 'vtAnsiString';
            vtCurrency: Result := Result + ',' + 'vtCurrency';
            vtVariant: Result := Result + ',' + 'vtVariant';
            vtInterface: Result := Result + ',' + 'vtInterface';
            vtWideString: Result := Result + ',' + 'vtWideString';
            vtInt64: Result := Result + ',' + 'vtInt64';
          else Result := Result + ',' + '<Unknown>';
          end;
        end;
      Delete(Result, 1, 1);
    end; { VarTypeName }procedure TForm1.Button1Click(Sender: TObject);
    const
      A = '1';
      B = '';
      C: WideString = '1';
      D: PChar = '1';
      I = 1;
      J = 256;
    begin
      Memo1.Lines.Add('---------------------');
      Memo1.Lines.Add(Format('A:%s(%d)', [VarTypeName([A]), SizeOf(A)]));
      Memo1.Lines.Add(Format('B:%s(%d)', [VarTypeName([B]), SizeOf(B)]));
      Memo1.Lines.Add(Format('C:%s(%d)', [VarTypeName([C]), SizeOf(C)]));
      Memo1.Lines.Add(Format('D:%s(%d)', [VarTypeName([D]), SizeOf(D)]));
      Memo1.Lines.Add(Format('I:%s(%d)', [VarTypeName([I]), SizeOf(I)]));
      Memo1.Lines.Add(Format('J:%s(%d)', [VarTypeName([J]), SizeOf(J)]));
    end;(*
    //可以肯定,所以的数据都是有类型的~~
    //因为类型是描述数据的存储方式~~
    //常量也有!~~
    //常量的类型通常是有由编译器根据最小存储空间自动匹配的~~
    const
      A = '1'; //Char
      B = ''; //stringconst
      I = 1; //Byte(1)
      J = 256; //Word(2)//为了使常量的类型可以控制~~
    //所以出现了类型常量~~
    const
      C: WideString = '1'; //WideString(4)
      D: PChar = '1'; //PChar(4)
    //虽然A、C、D都是同样的值,但他们类型却都不一样~~
    *)//这些基础的东西,最好自己去找答案,这样才能学得扎实~~
    //好了,其实这些都是规则,谁叫pascal不是你我发明的!~~
    //你能用var解决就用吧~~
      

  4.   

    同时 var变量不可以直接赋初始值