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

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

问题描述

从Java背景开始玩Swift,为什么要选择Struct而不是Class?似乎它们是同一回事,但Struct提供的功能较少.为什么选择它呢?

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演讲中的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).

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

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)的只写管道
  • Copying or comparing instances doesn't make sense (e.g., Window)
  • Instance lifetime is tied to external effects (e.g., TemporaryFile)
  • Instances are just "sinks"--write-only conduits to external state (e.g.CGContext)

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

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

另一方面,

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:

  • 该结构的主要目的是封装一些相对简单的数据值.
  • 可以合理地预期,当您分配或传递一个封装时,封装的值将被复制而不是被引用. 该结构的实例.
  • 该结构存储的任何属性本身就是值类型,也应该将其复制而不是引用.
  • 该结构无需从其他现有类型继承属性或行为.
  • The structure’s primary purpose is to encapsulate a few relatively simple data values.
  • It is reasonable to expect that the encapsulated values will be copied rather than referenced when you assign or pass around an instance of that structure.
  • Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced.
  • The structure does not need to inherit properties or behavior from another existing type.

结构良好的候选人包括:

Examples of good candidates for structures include:

  • 两个Double类型的几何形状的大小,可能封装了width属性和height属性.
  • 一种引用系列中范围的方法,可能封装了一个Int类型的start属性和一个length属性.
  • 3D坐标系中的一个点,可能封装了x,y和z属性,每个属性都是Double类型.
  • The size of a geometric shape, perhaps encapsulating a width property and a height property, both of type Double.
  • A way to refer to ranges within a series, perhaps encapsulating a start property and a length property, both of type Int.
  • A point in a 3D coordinate system, perhaps encapsulating x, y and z properties, each of type 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天全站免登陆