我们为什么要使用类而不是记录,反之亦然?

时间:2022-09-11 10:35:36

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.

我已经使用Delphi很长一段时间了,但是我不是来自CS背景,而是在工作中学习 - 主要来自我的Boss,并通过网络上的点点滴滴,用户指南,示例进行了扩充。等

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

现在我的老板是老学校,开始使用Pascal编程,并没有必要及时了解Delphi的最新变化。

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

就在最近,我一直想知道我们的核心技术之一是否“错误”。

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

我们的大多数应用程序都与MySQL连接。通常,我们将创建一个记录,其结构用于存储从DB读取的数据,这些记录将存储在TList中。通常,我们将有一个单元,用于定义应用程序中的各种记录,以及种子和读取记录的功能和过程。我们不使用此处概述的记录程序

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.

在回顾了一些例子之后,我开始想知道我们是否会更好地使用课程而不是记录,但我很难找到强有力的指导。

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

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

1 个解决方案

#1


36  

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

最大的区别是记录是值类型,类是引用类型。简而言之,这意味着:

  1. For a value type, when you use assignment, a := b, a copy is made. There are two distinct instances, a and b.
  2. 对于值类型,当您使用赋值时,a:= b,进行复制。有两个不同的实例,a和b。
  3. For a reference type, when you use assignment, a := b, both variables refer to the same instance. There is only one instance.
  4. 对于引用类型,当您使用赋值时,a:= b,两个变量都引用同一个实例。只有一个例子。

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.

这样做的主要结果是当您编写a.Field:= 42时会发生什么。对于记录,值类型,赋值a.Field会更改a中成员的值,但不会更改为b。那是因为a和b是不同的实例。但对于一个类,由于a和b都引用同一个实例,因此在执行a.Field:= 42之后,可以断言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.

更重要的是,您声明您的类型不使用方法。只包含数据且没有与之关联的方法的类型很可能最好用值类型表示。我不能肯定地说,但我的直觉告诉我原始开发者做出了正确的选择。

#1


36  

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

最大的区别是记录是值类型,类是引用类型。简而言之,这意味着:

  1. For a value type, when you use assignment, a := b, a copy is made. There are two distinct instances, a and b.
  2. 对于值类型,当您使用赋值时,a:= b,进行复制。有两个不同的实例,a和b。
  3. For a reference type, when you use assignment, a := b, both variables refer to the same instance. There is only one instance.
  4. 对于引用类型,当您使用赋值时,a:= b,两个变量都引用同一个实例。只有一个例子。

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.

这样做的主要结果是当您编写a.Field:= 42时会发生什么。对于记录,值类型,赋值a.Field会更改a中成员的值,但不会更改为b。那是因为a和b是不同的实例。但对于一个类,由于a和b都引用同一个实例,因此在执行a.Field:= 42之后,可以断言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.

更重要的是,您声明您的类型不使用方法。只包含数据且没有与之关联的方法的类型很可能最好用值类型表示。我不能肯定地说,但我的直觉告诉我原始开发者做出了正确的选择。