为什么我们应该使用类而不是记录,反之亦然? [英] Why should we use classes rather than records, or vice versa?

查看:169
本文介绍了为什么我们应该使用类而不是记录,反之亦然?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Delphi一段时间,而不是来自CS背景,我已经学会了在工作 - 主要是从我的老板,并增加了从网站,从网站拾取的用户和用户指南,示例等。

I've been using Delphi for quite some time now, but rather than coming from a CS background I have learnt "on the job" - mostly from my Boss, and augmented by bits and pieces picked up from the web, users guides, examples, etc.

现在我的老板是老学校,开始使用Pascal编程,并不一定跟上最新的Delphi的最新变化。

Now my boss is old school, started programming using Pascal, and hasn't necessarily kept up-to-date with the latest changes to Delphi.

最近我一直在想我们的核心技术是否错误。

Just recently I've been wondering whether one of our core techniques is "wrong".

我们的应用程序与MySQL接口。一般来说,我们将创建一个记录,该结构存储从数据库读取的数据,这些记录将存储在 TList 。通常,我们将有一个单元,定义我们在应用程序中的各种记录,以及种子和读取记录的函数和过程。我们不使用记录过程,例如此处

Most of our applications interface with MySQL. In general we will create a record with a structure to store data read from the DB, and these records will be stored in a TList. Generally we will have a unit that defines the various records that we have in an application, and the functions and procedures that seed and read the records. We don't use record procedures such as outlined here

查看一些例子后,我开始想知道我们是否会更好地使用 classes

After reviewing some examples I've started wondering whether we'd be better off using classes rather than records, but I'm having difficulty finding strong guidance either way.

我们正在处理的事情将是用户信息:名称,DOB,事件,事件类型。或时间表信息:小时,工作等...

The sort of thing that we are dealing with would be User information: Names, DOB, Events, Event Types. Or Timesheet information: Hours, Jobs, etc...

推荐答案

最大的区别是记录是,类是引用类型。简单来说,这意味着:

The big difference is that records are value types and classes are reference types. In a nutshell what this means is that:


  1. 对于值类型,当使用赋值时, a: b ,则复制。有两个不同的实例, a b

  2. 对于引用类型,当使用赋值 a:= b 时,两个变量都指向同一个实例。只有一个实例。

  1. For a value type, when you use assignment, a := b, a copy is made. There are two distinct instances, a and b.
  2. For a reference type, when you use assignment, a := b, both variables refer to the same instance. There is only one instance.

这样的主要后果是当你写 a.Field:= 42 。对于记录,值类型,赋值 a.Field 更改 a 中的成员的值,但不在 b 中。这是因为 a b 是不同的实例。但是对于一个类,由于 a b 都指向同一个实例,所以在执行 a.Field:= 42 您可以断言 b.Field = 42

The main consequence of this is what happens when you write a.Field := 42. For a record, the value type, the assignment a.Field changes the value of the member in a, but not in b. That's because a and b are different instances. But for a class, since a and b both refer to the same instance, then after executing a.Field := 42 you are safe to assert that b.Field = 42.

没有硬性规则说你应该总是使用值类型,或者总是使用引用类型。两个都有自己的位置。在一些情况下,将优选使用一种,并且在其它情况下,优选使用另一种。基本上,决定总是决定你想要的赋值运算符是什么意思。

There's no hard and fast rule that says that you should always use value types, or always use reference types. Both have their place. In some situations, it will be preferable to use one, and in other situations it will be preferable to use the other. Essentially the decision always comes down to a decision on what you want the assignment operator to mean.

你有一个现有的代码库,并且可能是熟悉它的程序员,做出特别的选择。除非你有一个令人信服的理由切换到使用引用类型,使更改几乎肯定会导致缺陷。和现有代码中的缺陷(切换到引用类型更改赋值运算符的含义),以及在将来编写的代码中(您和您的同事已经在特定上下文中开发了赋值运算符的含义的直觉,并且直觉会断开如果你切换)。

You have an existing code base, and presumably programmers familiar with it, that has made particular choices. Unless you have a compelling reason to switch to using reference types, making the change will almost certainly lead to defects. And defects both in the existing code (switch to reference type changes meaning of assignment operator), and in code you write in the future (you and your colleagues have developed intuition as to meaning of assignment operator in specific contexts, and that intuition will break if you switch).

此外,你声明你的类型不使用方法。一种仅由数据组成的类型,并且没有与其相关联的方法很可能最好由值类型表示。我不能肯定地说,但我的直觉告诉我,原来的开发者做出了正确的选择。

What's more, you state that your types do not use methods. A type that consists only of data, and has no methods associated with it is very likely best represented by a value type. I cannot say that for sure, but my instincts tell me that the original developers made the right choice.

这篇关于为什么我们应该使用类而不是记录,反之亦然?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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