TMemo(Button1),TForm(Button1)这什么这样转换也能成功?

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var a:TStringList;
    begin
      TForm(a).Caption := 'xxxoo'
    end;
    2.没有实例化情况下这样也不会出错。并把FORM1的标题改掉了?这是怎么回事?
    而加上
    procedure TForm1.Button1Click(Sender: TObject);
    var a:TStringList;
    begin
      a := TStringList.create();
      TForm(a).Caption := 'xxxoo'
    end;3.则出错了。
    这是为什么呢?
      

  2.   

    1、第一段代码以前有人讨论过,和编译器有关,不建议这样用
    2、第二段代码中TStringList类没有Caption属性成员
    TStringList - TStrings - TPersistent
    TForm - TCustomForm - TScrollingWinControl - TWinControl - TControl - TComponent - TPersistent
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    var a:TStringList;
    begin
      TForm(a).Caption := 'xxxoo'
    end;
    调试模式下反汇编代码:
    Unit1.pas.35: TForm(a).Caption := 'xxxoo' 
    0044E1B4 BAC8E14400 mov edx,$0044e1c8
    0044E1B9 E82EF3FDFF call TControl.SetText
    此时TControl.SetText的第一个参数eax即Form1对象,
    第二个参数edx指向常量字符串xxxoo,所以调用TControl.SetText就是设置Form1的Caption!
    --------------霸气侧露的分割线--------------procedure TForm1.Button1Click(Sender: TObject);
    var a:TStringList;
    begin
      a := TStringList.create();
      TForm(a).Caption := 'xxxoo'
    end;
    调试模式下反汇编代码:
    Unit1.pas.35: a := TStringList.create();
    0044E1B4 B201 mov dl,$01
    0044E1B6 A1381C4100 mov eax,[$00411c38]
    0044E1BB E87C52FBFF call TObject.Create
    Unit1.pas.36 TForm(a).Caption := 'xxxoo'
    0044E1C0 BAD4E14400 mov edx,$0044e1d4
    0044E1C5 E822F3FDFF call TControl.SetText
    前三行是创建TStringList的实例对象a,创建完成后eax为对象a,而不再是Form1,
    并且TStringList不是TControl的子孙类,所以就出错了!