严重注意:

使用记录类型的时候要严重注意,记录类型的元素初始化 并非我们想要的结果,如果是integer,currey当不赋值的时候,初始化并不为0,而是随机数,

boolean 类型默认为true,所以当记录类型的元素用不到的时候,一定要注释掉,而不是放再那里不赋值,如果你不赋值,系统也会自动给你赋值了

 

unit Unit18;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm18 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

/// <summary>
/// 纯类
/// </summary>
type TRenLei = class
  private
    Fname: string;
    Fage: Integer;
    Fmoney: Currency;
    Fsex: Boolean;
    procedure Setname(Value: string);
    procedure Setage(Value: Integer);
    procedure Setmoney(Value: Currency);
    procedure Setsex(Value: Boolean);
  public
    property name: string read Fname write Setname;
    property age: Integer read Fage write Setage;
    property money: Currency read Fmoney write Setmoney;
    property sex: Boolean read Fsex write Setsex;
end;

/// <summary>
/// 纯记录
/// </summary>
type TRenRecord = record
  name: string;
  age: Integer;
  money: Currency;
  sex: Boolean;
end;

/// <summary>
/// 特殊记录属性
/// </summary>
type TRenOther = record
  private
    Fname: string;
    Fage: Integer;
    Fmoney: Currency;
    Fsex: Boolean;
    procedure Setname(Value: string);
    procedure Setage(Value: Integer);
    procedure Setmoney(Value: Currency);
    procedure Setsex(Value: Boolean);
  public
    property name: string read Fname write Setname;
    property age: Integer read Fage write Setage;
    property money: Currency read Fmoney write Setmoney;
    property sex: Boolean read Fsex write Setsex;
end;

var
  Form18: TForm18;

implementation

{$R *.dfm}

procedure TForm18.Button1Click(Sender: TObject);
var
  MyRenLei: TRenLei;
begin
  MyRenLei := TRenLei.Create;
  try
    ShowMessage(MyRenLei.name);//
    ShowMessage(IntToStr(MyRenLei.age));//0
    ShowMessage(CurrToStr(MyRenLei.money));//0
    ShowMessage(BoolToStr(MyRenLei.sex,True));//False
  finally
    MyRenLei.Free;
  end;
end;

procedure TForm18.Button2Click(Sender: TObject);
var
  MyRenRecord: TRenRecord;
begin
  ShowMessage(MyRenRecord.name);//
  ShowMessage(IntToStr(MyRenRecord.age));//随机整数
  ShowMessage(CurrToStr(MyRenRecord.money));//随机小数,四位小数点
  ShowMessage(BoolToStr(MyRenRecord.sex,True));//True
end;

procedure TForm18.Button3Click(Sender: TObject);
var
  MyRenOther: TRenOther;
begin
  ShowMessage(MyRenOther.name);//
  ShowMessage(IntToStr(MyRenOther.age));//随机整数
  ShowMessage(CurrToStr(MyRenOther.money));//随机小数,四位小数点
  ShowMessage(BoolToStr(MyRenOther.sex,True));//True
end;

procedure TRenLei.Setname(Value: string);
begin
  Fname := Value;
end;

procedure TRenLei.Setage(Value: Integer);
begin
  Fage := Value;
end;

procedure TRenLei.Setmoney(Value: Currency);
begin
  Fmoney := Value;
end;

procedure TRenLei.Setsex(Value: Boolean);
begin
  Fsex := Value;
end;

procedure TRenOther.Setname(Value: string);
begin
  Fname := Value;
end;

procedure TRenOther.Setage(Value: Integer);
begin
  Fage := Value;
end;

procedure TRenOther.Setmoney(Value: Currency);
begin
  Fmoney := Value;
end;

procedure TRenOther.Setsex(Value: Boolean);
begin
  Fsex := Value;
end;

end.

 

 

posted on 2015-07-26 10:29  del88  阅读(11)  评论(0编辑  收藏  举报