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中测试通过。

  • 相关阅读:
    编程心得
    关于百分比的小花招
    vue2.0实现银行卡类型种类的选择
    如何运行vue项目(维护他人的项目)
    手把手教你用vue-cli构建一个简单的路由应用
    解决eclipse端口被占用的问题
    echarts统计图踩坑合集
    echarts如何设置背景图的颜色
    小程序获取的用户头像怎么做成圆形
    vue踩坑记-在项目中安装依赖模块npm install报错
  • 原文地址:https://www.cnblogs.com/pchmonster/p/2294035.html
Copyright © 2011-2022 走看看