zoukankan      html  css  js  c++  java
  • 对delphi的Create,inherited,AfterConstruction的流程了解及测试

    写了一个测试代码, 创建一个窗体拉上一个按钮控件,复制以下的代码

    1. unit Unit1;
    2. interface
    3. uses
    4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    5.   Dialogs, StdCtrls;
    6. type
    7.   TForm1 = class(TForm)
    8.     Button1: TButton;
    9.     procedure Button1Click(Sender: TObject);
    10.   private
    11.     { Private declarations }
    12.   public
    13.     { Public declarations }
    14.   end;
    15.   C1 = class //(TPersistent)
    16.   private
    17.     p1:string;
    18.   public
    19.     procedure Show;
    20.     procedure AfterConstruction; override;
    21.     procedure BeforeDestruction; override;
    22.     constructor Create; virtual;
    23.     procedure test(str: string);
    24.   end;
    25.   C2 = class(C1)
    26.   public
    27.     constructor Create; override;
    28.     constructor CreateNew; virtual;
    29.     procedure AfterConstruction; override;
    30.     procedure Show;
    31.   end;
    32.   C3 = class(c2)
    33.   end;
    34. var
    35.   Form1: TForm1;
    36. implementation
    37. {$R *.dfm}
    38. { C1 }
    39. procedure C1.AfterConstruction;
    40. begin
    41.   inherited AfterConstruction;
    42.   test('C1.AfterConstruction'); ;
    43. end;
    44. procedure C1.BeforeDestruction;
    45. begin
    46.   test('C1.BeforeDestruction'); ;
    47.   inherited BeforeDestruction;
    48. end;
    49. constructor C1.Create;
    50. begin
    51. inherited;
    52.   test('C1.Create');
    53. end;
    54. procedure C1.Show;
    55. begin
    56.   ShowMessage(p1);
    57. end;
    58. procedure C1.test(str: string);
    59. begin
    60.   ShowMessage(str);
    61. end;
    62. { C2 }
    63. procedure C2.AfterConstruction;
    64. begin
    65.   inherited;
    66.   ShowMessage('C2.AfterConstruction');
    67. end;
    68. constructor C2.Create;
    69. begin
    70.   inherited Create;
    71.   test('c2.Create'); ;
    72. end;
    73. constructor C2.CreateNew;
    74. begin
    75.   Create;
    76.   test('c2.CreateNew');
    77. end;
    78. procedure C2.Show;
    79. begin
    80.   inherited Show;
    81.   test('c2.show');
    82. end;
    83. procedure TForm1.Button1Click(Sender: TObject);
    84. var
    85.   a: c3;
    86.   b: c2;
    87.   c:c1;
    88.   begin
    89.   c:=c3.CreateNew;
    90.   c.p1:='aaa';
    91.   c.Show;
    92.   c.test('aaaaa');
    93. end;
    94. end.

    单击按钮时,依次显示:

    C1.Create    // 运行了c:=c3.CreateNew; inherited Create;

    C2.Create    //Create;

    C2.CreateNew

    C1.AfterConstruction    C2.AfterConstruction 里的inherited 

    C2.AfterConstruction

    aaa

    aaaaa

     

    在对象创建前会自动调用AfterConstruction,资料参考空间里之前发布的文章

    靓点博客 http://www.cnblogs.com/mlog 或 http://blog.csdn.net/cml2030
  • 相关阅读:
    349. Intersection of Two Arrays
    346. Moving Average from Data Stream
    345. Reverse Vowels of a String
    344. Reverse String
    342. Power of Four
    POJ2823 Sliding Window
    《STL源码剖析》笔记
    [jobdu]扑克牌顺子
    [jobdu]第一个只出现一次的字符
    [jobdu]包含min函数的栈
  • 原文地址:https://www.cnblogs.com/mlog/p/2456390.html
Copyright © 2011-2022 走看看