Overview of structure and class
A structure can be regarded as a lightweight class.
In terms of the way of use, a structure is defined and can be used directly, while a class must create an object of that class before it can be used.
In terms of memory management, the memory usage of the structure is automatically maintained by the application, while the class needs to manually create the objects of the class and release them when we no longer need them.
Note: compared with the lightweight structure, the heavyweight class can have more complex functions, and at the same time, the class more embodies the object-oriented idea.
Definition of structure and class
The keywords used in the definition of structure and class are different: record and class.
type {Use record Define a structure} MyRecord = record {Defining the properties of a structure} name: String; {The process of defining a structure} procedure showName; {How to define a structure} function getName(): String; end; {Use class Define a class} MyClass = class {Defining the properties of a class} name: String; {The process of defining a class} procedure showName; {Methods for defining classes} function getName(): String; end;
Concrete realization of process and method in structure and class
{ MyRecord } function MyRecord.getName: String; begin Result := name; end; procedure MyRecord.showName; begin showMessage(name); end; { MyClass } function MyClass.getName: String; begin Result := name; end; procedure MyClass.showName; begin showMessage(name); end; end.
Structure and class usage
procedure TForm1.Button1Click(Sender: TObject); var {After the structure is declared here, it can be used directly} mRecord: MyRecord; {You also need to create an object of this class after declaring it} mClass: MyClass; begin {Property, process and method in operation structure} mRecord.name :='mRecord_name'; showMessage(mRecord.name); showMessage(mRecord.getName); mRecord.showName(); {You need to create the object of the class first} mClass := MyClass.Create(); {Properties, procedures and methods in operation classes} mClass.name :='mClass_name'; showMessage(mClass.name); showMessage(mClass.getName); mClass.showName(); end;
---------------------This article is from the CSDN blog of GUI A Mei. Please click https://blog.csdn.net/u014046591/article/details/48937277?utm_source=copy