zoukankan      html  css  js  c++  java
  • 数据类型之记录(record)

    在Object Pascal中用户自定的结构成为记录。它相当于C语言中的struct,Visual Basic中的Type

    记录(record)是一个集合,它把一组相关数据聚集在一个存储单元里,记录中的每个元素称作字段。

    记录的声明和简单访问示例:

    type
      MailingListRecord = record   { 声明记录用关键字record}
        FirstName: string;
        LastName: string;
        Address: string;
        City: string;
        State: string;
        Zip: Integer;
      end;
    var
      MLRecord: MailingListRecord;  { 定义一个该记录的变量}
    begin
      { 当使用记录时,用小圆点来访问它的字段}
      MLRecord.FirstName := 'Bruce';
      MLRecord.LastName := 'Reisdorph';
      MLRecord.Address := '123 Inspiration Pt.';
      MLRecord.City := 'Merced';
      MLRecord.State := 'CA';
      MLRecord.Zip := 99999;
    
      { 使用with语句来简化上面的字段的输入}
      { with表示“用这个对象'MLRecord'做下列”}
      with MLRecord do
      begin
        FirstName := 'Bruce';
        LastName := 'Reisdorph';
        Address := '123 Inspiration Pt.';
        City := 'Merced';
        State := 'CA';
        Zip := 99999;
      end;
      { with可以减少键入量,同时也使程序可读性更强}
    end;

    当然记录也可以有数组,声明和使用记录数组不是太复杂,具体代码如下:

    type
      MailingListRecord = record   { 声明记录用关键字record}
        FirstName: string;
        LastName: string;
        Address: string;
        City: string;
        State: string;
        Zip: Integer;
      end;
    var
      MLRecord: array[0..9] of MailingListRecord;  { 定义一个记录数组}
    begin
      { 当使用记录时,用小圆点来访问它的字段}
      MLRecord[0].FirstName := 'Bruce';
      MLRecord[0].LastName := 'Reisdorph';
      MLRecord[0].Address := '123 Inspiration Pt.';
      MLRecord[0].City := 'Merced';
      MLRecord[0].State := 'CA';
      MLRecord[0].Zip := 99999;
    
      { 使用with语句来简化上面的字段的输入}
      { with表示“用这个对象'MLRecord'做下列”}
      with MLRecord[1] do
      begin
        FirstName := 'Bruce';
        LastName := 'Reisdorph';
        Address := '123 Inspiration Pt.';
        City := 'Merced';
        State := 'CA';
        Zip := 99999;
      end;
      { with可以减少键入量,同时也使程序可读性更强}
    end;

    当然Object Pascal也支持可变记录,以后我们再讨论吧。

    以上代码均在Delphi7中测试通过。

  • 相关阅读:
    Go对比其他语言新特性2(函数、包、错误处理)
    计算机基础知识
    GO的下载和环境配置,Goland编译器的配置和初始化项目
    软件工程第五次作业
    软件工程第四次作业
    软件工程第三次作业
    软件工程第二次作业
    软件工程第一次作业
    软件工程结对第二次作业
    软件工程结对第一次作业
  • 原文地址:https://www.cnblogs.com/pchmonster/p/2294035.html
Copyright © 2011-2022 走看看