【Delphi 基础知识 6】常用的数据类型

目录

  • 1、基本数据类型
  • 2、结构化数据类型
  • 3、指针类型
  • 4、动态数组(Dynamic Array)
  • 5、集合类型(TList)
  • 6、关联数组(Associative Array)
  • 7、类(Class)
  • 8、接口(Interface)
  • 9、枚举类型(Enum)
  • 10、集合类型(Set)
  • 11、Procedural(过程类型)
  • 12、Variant 类型(Variant)
  • 13、Extended 类型

Delphi 支持多种数据类型,以下是一些常见的 Delphi 数据类型及其示例:

1、基本数据类型

  • Integer(整数):

    var
      myInteger: Integer;
    begin
      myInteger := 42;
    end;
    
  • Char(字符):

    var
      myChar: Char;
    begin
      myChar := 'A';
    end;
    
  • Boolean(布尔):

    var
      myBoolean: Boolean;
    begin
      myBoolean := True;
    end;
    
  • Real(实数):

    var
      myReal: Real;
    begin
      myReal := 3.14;
    end;
    
  • String(字符串):

    var
      myString: String;
    begin
      myString := 'Hello, Delphi!';
    end;
    

2、结构化数据类型

  • Array(数组):

    	var
    	  myArray: array [0..4] of Integer;
    	begin
    	  myArray[0] := 10;
    	  myArray[1] := 20;
    	  // ...
    	end;
    
  • Record(记录):

    type
      TPerson = record
        FirstName: String;
        LastName: String;
        Age: Integer;
      end;
    
    var
      person: TPerson;
    begin
      person.FirstName := 'John';
      person.LastName := 'Doe';
      person.Age := 30;
    end;
    

3、指针类型

  • Pointer(指针):

    var
      myPointer: ^Integer;
      myValue: Integer;
    begin
      New(myPointer); // 分配内存
      myValue := 42;
      myPointer^ := myValue;
      Dispose(myPointer); // 释放内存
    end;
    

4、动态数组(Dynamic Array)

	var
	  myDynamicArray: array of Integer;
	begin
	  SetLength(myDynamicArray, 5);
	  myDynamicArray[0] := 10;
	  myDynamicArray[1] := 20;
	  // ...
	end;

5、集合类型(TList)

uses
  Classes;

var
  myList: TList;
begin
  myList := TList.Create;
  myList.Add(10);
  myList.Add(20);
  // ...
  myList.Free; // 释放内存
end;

6、关联数组(Associative Array)

var
  myMap: TDictionary;
begin
  myMap := TDictionary.Create;
  myMap.Add('One', 1);
  myMap.Add('Two', 2);
  // ...
  myMap.Free; // 释放内存
end;

7、类(Class)

type
  TPerson = class
  private
    FName: String;
    FAge: Integer;
  public
    constructor Create(Name: String; Age: Integer);
    property Name: String read FName write FName;
    property Age: Integer read FAge write FAge;
  end;

constructor TPerson.Create(Name: String; Age: Integer);
begin
  FName := Name;
  FAge := Age;
end;

var
  person: TPerson;
begin
  person := TPerson.Create('John', 30);
  // 使用 person 对象
  person.Free; // 释放对象内存
end;

8、接口(Interface)

type
  IPrintable = interface
    procedure Print;
  end;

  TMyClass = class(TInterfacedObject, IPrintable)
  public
    procedure Print;
  end;

procedure TMyClass.Print;
begin
  writeln('Printing from TMyClass');
end;

var
  myObj: IPrintable;
begin
  myObj := TMyClass.Create;
  myObj.Print;
end;

9、枚举类型(Enum)

type
  TDayOfWeek = (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday);

var
  today: TDayOfWeek;
begin
  today := Monday;
end;

10、集合类型(Set)

type
  TDays = set of TDayOfWeek;

var
  workingDays: TDays;
begin
  workingDays := [Monday, Tuesday, Wednesday, Thursday, Friday];
end;

11、Procedural(过程类型)

type
  TProcedure = procedure;

var
  myProcedure: TProcedure;
begin
  myProcedure := @MyFunction;
  myProcedure;
end;

procedure MyFunction;
begin
  // 实现具体的过程
end;

12、Variant 类型(Variant)

var
  myVariant: Variant;
begin
  myVariant := 10; // 可以存储不同类型的值
  myVariant := 'Hello';
end;

13、Extended 类型

var
  myExtended: Extended;
begin
  myExtended := 3.14159265358979323846;
end;

你可能感兴趣的:(100天精通Delphi,基础知识)