我什么时候应该在 Delphi 中使用增强的记录类型而不是类?
Delphi 2006 引入了新的记录功能,使它们更加“面向对象”。
在哪些情况下记录类型比类类型更适合设计? 使用这些记录类型有什么好处?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这些功能在 Delphi 8 和 2005 中也可用。
主要指南:如果您有疑问,请使用类。
对于其余的,您必须了解主要区别:类对象始终通过引用使用,并通过调用构造函数创建。
记录的内存管理和分配与基本类型(即整数、双精度)相同。 这意味着它们按值传递给方法(除非使用 var)。 此外,您不需要释放记录,这就是它们支持运算符重载的原因。 但没有继承或虚拟方法等。新记录可以有一个构造函数,但它的使用是可选的。
使用记录的主要领域和标准:
当处理来自 Win32 API 的结构时
当类型没有标识时(因为赋值意味着复制)< /p>
当实例不太大时(复制大记录变得昂贵)
当构建值类型时,其行为应该模仿数字类型。 例如日期时间、复数、向量等。运算符重载是一个很好的功能,但不要将其作为决定因素。
从效率角度来看,不要过度这样做:
最后,使用类或记录的规则与早期版本的 Delphi 相比并没有真正改变。
I think those features were also available in Delphi 8 and 2005.
Main guideline: if you're in doubt, use a class.
For the rest you have to understand the main difference: Class Objects are always used through a reference, and are created by calling a Constructor.
The memory management and allocation for Records is the same as for the basic types (ie integer, double). That means that they are passed to methods by value (unless var is used). Also you don't need to Free records, and that's the reason they support operator overloading. But no inheritance or virtual methods etc. The new Records can have a constructor but it's use is kind of optional.
The main areas and criteria for using records:
when dealing with structs from the Win32 API
when the types don't have identity (because assignment means copying)
when the instances aren't too large (copying big records becomes expensive)
when building value types, whose behaviour should mimic the numerical types. Examples are DateTime, Complex Numbers, Vectors etc. And then operator overloading is a nice feature, but don't make that the deciding factor.
And efficiency-wise, don't overdo this:
And finally, the rules for using a class or a records haven't really changed form the earlier versions of Delphi.
除了其他答案(运算符重载、轻量级值类型)之外,最好让枚举器记录而不是类。 由于它们是在堆栈上分配的,因此无需构造和析构它们,这也消除了编译器在类类型枚举器周围放置的隐藏 try..finally 块的需要。
请参阅http://hallvards.blogspot.com/2007/10 /more-fun-with-enumerators.html 了解更多信息。
In addition to the other answers (operator overloading, lightweight value types), it's a good idea to make your enumerators records instead of classes. Since they're allocated on the stack, there's no need to construct and destruct them, which also removes the need for the hidden try..finally block that the compiler places around class-type enumerators.
See http://hallvards.blogspot.com/2007/10/more-fun-with-enumerators.html for more information.
您可以使用运算符重载(如隐式转换)。 您也可以在 Delphi 2007+ 或 2006.NET 上的对象上执行此操作,但只能在 2006 win32 上的这些记录上执行。
You can use operator overloading (like implicit conversions). This you can do on Delphi 2007+ or 2006.NET on objects too, but only on these records on 2006 win32.
您有记录、对象和类。
记录从turbo pascal 1开始可用。它们是轻量级的,能够具有属性和方法,但它们不支持继承,返回记录的函数存在一些问题。 如果这些记录有方法,有时会出现内部错误:
如果我是正确的,则使用 Turbo pascal 5 引入对象。 然后他们提供了一种使用 pascal 进行面向对象的方法。 随着 Delphi 的引入,它们或多或少地被弃用了,但您仍然可以使用它们。 对象可以实现接口。
类是用 Delphi 1 引入的,也是最通用的。 它们实现接口并支持继承。 但每个类变量都是一个隐藏指针。 这意味着需要在堆上创建类。 幸运的是,这个过程大部分是隐藏的。
下表列出了三者之间的差异。 我添加了完成的接口。
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:
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.