为什么选择结构而不是类? [英] Why Choose Struct Over Class?

查看:35
本文介绍了为什么选择结构而不是类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

玩 Swift,来自 Java 背景,为什么要选择 Struct 而不是 Class?似乎它们是同一回事,结构提供较少的功能.为什么选择它?

Playing around with Swift, coming from a Java background, why would you want to choose a Struct instead of a Class? Seems like they are the same thing, with a Struct offering less functionality. Why choose it then?

推荐答案

根据非常流行的 WWDC 2015 演讲 Protocol Oriented Programming in Swift (视频转录),Swift 提供了许多特性,使结构在许多情况下比类更好.

According to the very popular WWDC 2015 talk Protocol Oriented Programming in Swift (video, transcript), Swift provides a number of features that make structs better than classes in many circumstances.

如果结构相对较小且可复制,则结构更可取,因为复制比对同一实例的多个引用更安全,就像在类中发生的那样.当将变量传递给许多类和/或在多线程环境中时,这一点尤其重要.如果您始终可以将变量的副本发送到其他地方,那么您就不必担心其他地方会更改您下方变量的值.

Structs are preferable if they are relatively small and copiable because copying is way safer than having multiple references to the same instance as happens with classes. This is especially important when passing around a variable to many classes and/or in a multithreaded environment. If you can always send a copy of your variable to other places, you never have to worry about that other place changing the value of your variable underneath you.

使用 Structs,无需担心内存泄漏或多个线程争相访问/修改变量的单个实例.(对于技术性更强的人来说,例外情况是在闭包内捕获结构时,因为它实际上是在捕获对实例的引用,除非您明确将其标记为要复制).

With Structs, there is much less need to worry about memory leaks or multiple threads racing to access/modify a single instance of a variable. (For the more technically minded, the exception to that is when capturing a struct inside a closure because then it is actually capturing a reference to the instance unless you explicitly mark it to be copied).

类也可能变得臃肿,因为一个类只能从一个超类继承.这鼓励我们创建巨大的超类,其中包含许多只是松散相关的不同能力.使用协议,尤其是使用协议扩展,您可以为协议提供实现,这样您就无需使用类来实现此类行为.

Classes can also become bloated because a class can only inherit from a single superclass. That encourages us to create huge superclasses that encompass many different abilities that are only loosely related. Using protocols, especially with protocol extensions where you can provide implementations to protocols, allows you to eliminate the need for classes to achieve this sort of behavior.

演讲列出了首选课程的这些场景:

The talk lays out these scenarios where classes are preferred:

  • 复制或比较实例没有意义(例如,Window)
  • 实例生命周期与外部效果相关(例如,TemporaryFile)
  • 实例只是接收器"——通向外部状态(例如 CGContext)的只写管道

这意味着结构应该是默认的,而类应该是后备.

It implies that structs should be the default and classes should be a fallback.

另一方面,The Swift Programming Language 文档有些矛盾:

On the other hand, The Swift Programming Language documentation is somewhat contradictory:

结构实例总是按值传递,而类实例总是通过引用传递.这意味着他们是适合不同类型的任务.当您考虑数据时项目所需的结构和功能,决定每个数据结构应该定义为一个类还是一个结构.

Structure instances are always passed by value, and class instances are always passed by reference. This means that they are suited to different kinds of tasks. As you consider the data constructs and functionality that you need for a project, decide whether each data construct should be defined as a class or as a structure.

作为一般准则,当一个或多个这些条件中的一个适用:

As a general guideline, consider creating a structure when one or more of these conditions apply:

  • 该结构的主要目的是封装一些相对简单的数据值.
  • 当您分配或传递一个被封装的值时,期望被复制而不是被引用是合理的该结构的实例.
  • 结构存储的任何属性本身都是值类型,也应该被复制而不是被引用.
  • 该结构不需要从另一个现有类型继承属性或行为.

结构良好的候选示例包括:

Examples of good candidates for structures include:

  • 几何形状的大小,可能封装了宽度属性和高度属性,两者都是 Double 类型.
  • 一种引用系列内范围的方法,可能封装了一个起始属性和一个长度属性,两者都是 Int 类型.
  • 3D 坐标系中的一个点,可能封装 x、y 和 z 属性,每个属性都是 Double 类型.

在所有其他情况下,定义一个类,并创建该类的实例通过引用进行管理和传递.在实践中,这意味着大多数自定义数据结构应该是类,而不是结构.

In all other cases, define a class, and create instances of that class to be managed and passed by reference. In practice, this means that most custom data constructs should be classes, not structures.

这里声称我们应该默认使用类并仅在特定情况下使用结构.最终,您需要了解值类型与引用类型在现实世界中的含义,然后才能就何时使用结构体或类做出明智的决定.此外,请记住,这些概念总是在不断发展,并且 Swift 编程语言文档是在面向协议的编程演讲之前编写的.

Here it is claiming that we should default to using classes and use structures only in specific circumstances. Ultimately, you need to understand the real world implication of value types vs. reference types and then you can make an informed decision about when to use structs or classes. Also, keep in mind that these concepts are always evolving and The Swift Programming Language documentation was written before the Protocol Oriented Programming talk was given.

这篇关于为什么选择结构而不是类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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