zoukankan      html  css  js  c++  java
  • 继承类的构造和析构方法

     1type
     2  TPerson = class//(Tobject)  //默认是继承 Tobject的
     3  private
     4    { Private declarations }
     5  public
     6  Name: String;
     7  Tall: Integer;
     8  Age: Integer;
     9  WaistLine: integer;
    10
    11    { Public declarations }
    12  end;
    13procedure TForm1.Button1Click(Sender: TObject);
    14VAR
    15  APerson: Tperson;
    16begin
    17  APerson := Tperson.Create; //必须用继承 TObject的构造方法
    18  APerson.Name := '涂磊';
    19  APerson.Tall := 159;     IntToStr(APerson.Tall);
    20  APerson.Age := 21;
    21  APerson.WaistLine := 25;
    22  ShowMessage(APerson.Name + #13#10 + '身高= ' + IntToStr(APerson.Tall) + #13#10 + '年龄= ' + IntToStr(APerson.Age)
    23  + #13#10 + '腰围= ' + IntToStr(APerson.WaistLine));
    24  APerson.Free; //必须用 TObject的析构方法   比Aperson.Destroy要好,因为Free 彻底清除了该对象Aperson,而Destroy只是把与对象
    25  //属性的 链接删除,并没有清除Aperson的对象实体
    26end;
    27
    28procedure TForm1.Button2Click(Sender: TObject);
    29VAR
    30  AHuman: THuman;
    31begin
    32  //AHuman := THuman.Create; //没有继承 TObject的构造方法,也不能用该方法
    33  AHuman.Name := '涂磊';
    34  AHuman.Tall := 159;     IntToStr(AHuman.Tall);
    35  AHuman.Age := 21;
    36  AHuman.WaistLine := 25;
    37  ShowMessage(AHuman.Name + #13#10 + '身高= ' + IntToStr(AHuman.Tall) + #13#10 + '年龄= ' + IntToStr(AHuman.Age)
    38  + #13#10 + '腰围= ' + IntToStr(AHuman.WaistLine));
    39 // AHuman.Free; //也不能用该TObject的析构方法
    40end;
    41
    42

     字段(Field)与对象引用(object reference)的实现
     1type
     2  THand = class(TObject)
     3  public
     4    Length: integer;
     5    Thick: integer;
     6    Color: Tcolor;
     7    { Public declarations }
     8  end;
     9type
    10  TFoot = Object
    11  public
    12    Length: integer;
    13    Thick: integer;
    14    Color: Tcolor;
    15    { Public declarations }
    16  end;
    17type
    18  TPerson = class(TObject)
    19  public
    20    LHand: THand;    //Object reference ,Class的类
    21    LFoot: TFoot;    //Object Instance ,Object的类
    22    LTall: integer;  //一般字段
    23    { Public declarations }
    24  end;
    25
    26type
    27  TForm1 = class(TForm)
    28    Button1: TButton;
    29    Label1: TLabel;
    30    Label2: TLabel;
    31    procedure Button1Click(Sender: TObject);
    32  private
    33    { Private declarations }
    34  public
    35    { Public declarations }
    36  end;
    37procedure TForm1.Button1Click(Sender: TObject);
    38VAR
    39  tulater: TPerson;
    40begin
    41  tulater := TPerson.Create;
    42    tulater.LTall := 180;
    43
    44    tulater.LHand :=  THand.Create;    //对象引用(Object reference )
    45    tulater.LHand.Length := 100;
    46    tulater.LHand.Thick := 6;
    47    tulater.LHand.Color := clYellow;
    48
    49    tulater.LFoot.Length := 130;
    50    tulater.LFoot.Thick := 8;
    51    tulater.LFoot.Color := clBlue;
    52    label1.Color := tulater.LHand.Color;
    53    label2.Color := tulater.LFoot.Color;
    54    ShowMessage(
    55                ' tulater ' + #13 +
    56                ' 身高 = ' + IntToStr(tulater.LTall) + #13 +
    57                ' 手长 = ' + IntToStr(tulater.LHand.Length) + #13 +
    58                ' 手粗 = ' + IntToStr(tulater.LHand.Thick) + #13 +
    59                ' 脚长 = ' + IntToStr(tulater.LFoot.Length) + #13 +
    60                ' 脚粗 = ' + IntToStr(tulater.LFoot.Color) + #13 
    61                );
    62    tulater.LHand.Free;       //对象解除引用
    63  tulater.Free;
    64end;
    65
    66
  • 相关阅读:
    Running ASP.NET Applications in Debian and Ubuntu using XSP and Mono
    .net extjs 封装
    ext direct spring
    install ubuntu tweak on ubuntu lts 10.04,this software is created by zhouding
    redis cookbook
    aptana eclipse plugin install on sts
    ubuntu open folderpath on terminal
    ubuntu install pae for the 32bit system 4g limited issue
    EXT Designer 正式版延长使用脚本
    用 Vagrant 快速建立開發環境
  • 原文地址:https://www.cnblogs.com/dreamszx/p/1572012.html
Copyright © 2011-2022 走看看