简体   繁体   English

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

[英]Why should we use classes rather than records, or vice versa?

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. 我们的大多数应用程序都与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 . 通常,我们将创建一个record ,其结构用于存储从DB读取的数据,这些记录将存储在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 我们不使用此处概述的记录程序

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. 在回顾了一些例子之后,我开始想知道我们是否会更好地使用classes而不是记录,但是我很难找到强有力的指导。

The sort of thing that we are dealing with would be User information: Names, DOB, Events, Event Types. 我们正在处理的事情是用户信息:名称,DOB,事件,事件类型。 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. For a value type, when you use assignment, a := b , a copy is made. 对于值类型,当您使用赋值时, a := b ,进行复制。 There are two distinct instances, a and b . 有两个不同的实例, ab
  2. For a reference type, when you use assignment, a := b , both variables refer to the same instance. 对于引用类型,当您使用赋值时, a := b ,两个变量都引用同一个实例。 There is only one instance. 只有一个例子。

The main consequence of this is what happens when you write a.Field := 42 . 这样做的主要结果是当你写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 . 对于记录,值类型,赋值a.Field更改a成员的值,但不更改b中的成员值。 That's because a and b are different instances. 那是因为ab是不同的实例。 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 . 但是对于一个类,由于ab都引用同一个实例,所以在执行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. 我不能肯定地说,但我的直觉告诉我原始的开发者做出了正确的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM