我应该何时在Delphi中使用增强型记录类型而不是类? [英] When should I use enhanced record types in Delphi instead of classes?

查看:223
本文介绍了我应该何时在Delphi中使用增强型记录类型而不是类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi 2006 为记录引入了新的功能,使它们更加面向对象'。



在哪种情况下,记录类型更适合于设计而不是类类型?
它必须使用这些记录类型的哪些优势?

解决方案

p>

自从turbo pascal 1以来就有记录。它们是轻量级的,能够具有属性和方法,但是它们不支持继承,返回记录的函数有一些问题。如果这些记录有方法,这有时会产生内部错误:

  type 
TRec = record
function Method1:整数;
end;

function Func:TRec;


程序测试;
var
x:TRec;

begin
Func.Method1; //有时崩溃编译器
//循环:
x:= Func;
x.Method1; // Works
end;

如果我是正确的,引入turbo pascal 5对象。他们然后提供了一种用帕斯卡的OO的方法。它们或多或少被Delphi的引入而废弃,但你仍然可以使用它们。对象可以实现接口。



类是用Delphi 1和最多才多艺的。它们实现接口和支持继承。但每个类变量是一个隐藏的指针。这意味着需要在堆上创建类。幸运的是,这个过程大多是隐藏的。



下面是一个表,三者之间的差异。我添加了完成界面。

  | Class | Object | Record | Interface | 
------------------ | --------------------------- - |
是指针吗? | y | n | n | y |
Inheritance | y | y | n | y |
Helpers | y | n | y | n |
Impl。接口| y | y | n | - |
Visibility | y | y | n | n |
方法| y | y | y | y |
Fields | y | y | y | n |
属性| y | y | y | y |
Consts | y | y | y | n |
Types | y | y | y | n |
Variants | n | n | y | n |
Virtual | y | n | y | - |
------------------ | --------------------------- - |


Delphi 2006 introduced new capabilities for records, making them more 'object-oriented'.

In which situations is the record type more appropriate for a design than a class type? Which advantage does it have to use these record types?

解决方案

You have records, objects and classes.

Records are available since turbo pascal 1. They are lightweight, capable of having properties and methods, but they do not support inheritance, There are some issues with functions that return records. If these records have methods this sometimes gives internal errors:

type
  TRec = record 
    function Method1: Integer;
  end;

function Func: TRec;


procedure Test;
var
  x : TRec;

begin
  Func.Method1; // Sometimes crashes the compiler
  // Circumvention:
  x := Func;
  x.Method1; // Works
end;

Objects are introduced with turbo pascal 5 if I'm correct. They then provided a way for OO with pascal. They are more or less deprecated with the introduction of Delphi, but you can still use them. Objects can implement interfaces.

Classes are introduced with Delphi 1 and the most versatile. They implement interfaces and support inheritance. But each class variable is a hidden pointer. This means that classes need to be created on the heap. Luckily this process is mostly hidden.

Below is a table with the differences between the three. I added the interface for completion.

                  |Class|Object|Record|Interface|
------------------|-----------------------------|
Are pointers?     |  y  |  n   |  n   |    y    |
Inheritance       |  y  |  y   |  n   |    y    |
Helpers           |  y  |  n   |  y   |    n    |
Impl. Interface   |  y  |  y   |  n   |    -    |
Visibility        |  y  |  y   |  n   |    n    |
Method            |  y  |  y   |  y   |    y    |
Fields            |  y  |  y   |  y   |    n    | 
Properties        |  y  |  y   |  y   |    y    |
Consts            |  y  |  y   |  y   |    n    |
Types             |  y  |  y   |  y   |    n    |
Variants          |  n  |  n   |  y   |    n    |
Virtual           |  y  |  n   |  y   |    -    |
------------------|-----------------------------|

这篇关于我应该何时在Delphi中使用增强型记录类型而不是类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆